当前位置:网站首页>[数组]BM94 接雨水问题-较难
[数组]BM94 接雨水问题-较难
2022-06-27 03:33:00 【51CTO】
BM94 接雨水问题
描述
给定一个整形数组arr,已知其中所有的值都是非负的,将这个数组看作一个柱子高度图,计算按此排列的柱子,下雨之后能接多少雨水。(数组以外的区域高度视为0)![[数组]BM94 接雨水问题-较难_双指针](/img/2b/1934803060d65ea9139ec489a2c5f5.png)
数据范围:数组长度 ,数组中每个值满足
,保证返回结果满足
要求:时间复杂度
示例1
输入:
复制返回值:
复制说明:
示例2
输入:
复制返回值:
题解
辅助数组解法
思路:
假设数组为a,对于任意下标i,它能盛水的容量取决于它左右两边最高的2根中低的那一根,假设该高度为h,盛水的容量则为h - a[i]。我们只要找出对于任意下表i,它左右最高
步骤如下:
- 试用2个数组max_left[i]和max_right[i]分别代表到下标i的时候,左边[0,i-1]最高的柱子,以及从右往左[n-1,i+1]最高的柱子,遍历数组,先求解这两个数组
- 再次遍历数组,当到达下标i的时候,该柱子盛水的容量为v = min(max_left[i],max_right[i]) - arr[i]
- 将第二步进行累计即可得到答案
代码如下:
// https://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295
using
namespace
std;
long
long
maxWater(
vector
<
int
>
&
arr)
{
if (
arr.
size()
<=
2)
{
return
0;
}
std::vector
<
int
>
max_left(
arr.
size(),
0);
std::vector
<
int
>
max_right(
arr.
size(),
0);
for (
int
i
=
1;
i
<
arr.
size();
++
i)
{
max_left[
i]
=
std::max(
max_left[
i
-
1],
arr[
i
-
1]);
}
for (
int
i
=
arr.
size()
-
2;
i
>=
0;
--
i)
{
max_right[
i]
=
std::max(
max_right[
i
+
1],
arr[
i
+
1]);
}
int
res
=
0;
for (
int
i
=
0;
i
<
arr.
size();
++
i)
{
int
h
=
std::min(
max_left[
i],
max_right[
i]);
if (
h
>
arr[
i])
{
res
+= (
h
-
arr[
i]);
}
}
return
res;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
双指针解法
在上面的解法中我们使用了2个辅助数组来存放某一下标i的左右最大高度。那么我们可不可以在不使用辅助数组的情况下知道下标i的左右最大高度呢?其实思考一下就很容易知道了,使用双指针可以很容易的写出答案。
步骤:
- 使用2个变量left_hight和right_hight分别代码最左和最右的最大高度
- 使用2个指针i和k分别从0和n-1的位置开始遍历
- 如果a[i] < left_hgith && a[i] < right_hight,那么索引为i的位置可以容纳min(left_hight,right_hight)-a[i]的水量
- 如果a[i] >= left_hight && a[i] <= right_hight,将i右移
- 如果a[i] >= left_hight && a[i] >= right_hight,将k左移
long
long
maxWater(
const
vector
<
int
>
&
arr)
{
if (
arr.
size()
<=
2)
{
return
0;
}
// 左右双指针
int
left
=
0;
int
right
=
arr.
size()
-
1;
// 在[left,right]区间外左边最高、右边最高
int
left_max
=
0;
int
right_max
=
0;
int
res
=
0;
while (
left
<=
right)
// 遍历整个数组
{
// 获取短边高度
int
min_hight
=
std::min(
left_max,
right_max);
res
+=
std::max(
0,
min_hight
-
std::min(
arr[
left],
arr[
right]));
// 累加当前索引可以盛水的容量
left_max
=
std::max(
left_max,
arr[
left]);
// 更新左高
right_max
=
std::max(
right_max,
arr[
right]);
// 更新右高
if (
arr[
left]
<
arr[
right])
// 更新索引,将较短的那个边往中间移动
{
left
++;
}
else
{
right
--;
}
}
return
res;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
边栏推荐
- 2021:adavqa: overlapping language priors with adapted margin cosine loss *
- TP5 spreadsheet excel table export
- [promise I] introduction of promise and key issues of hand rolling
- Geometric distribution (a discrete distribution)
- 实践 DevOps 时,可能面临的六大挑战
- ERP demand and sales management Kingdee
- 再探Handler(下)(Handler核心原理最全解析)
- Servlet and JSP final review examination site sorting 42 questions and 42 answers
- 2021:Greedy Gradient Ensemble for Robust Visual Question Answering
- jmeter分布式压测
猜你喜欢

2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验

How can e-commerce products be promoted and advertised on Zhihu?

2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri

2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers

2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering

MySql的开发环境

2021:passage retrieval for outside knowledgevisual question answering

2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答

Web development framework - Express (installation and use, static hosting, routing processing, use of Middleware)

电商产品如何在知乎上进行推广和打广告?
随机推荐
文旅灯光秀打破时空限制,展现景区夜游魅力
Uni app's uparse rich text parsing perfectly parses rich text!
2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
Nacos调用微服务两个问题:1.Load balancer does not contain an instance for the service 2.Connection refused
2022 operation of simulated examination platform for tea artist (Senior) work license question bank
语义化版本 2.0.0
Career outlook, money outlook and happiness outlook
2021:Zero-shot Visual Question Answering using Knowledge Graphs使用知识图的零次视觉问答
静态时序分析-OCV和time derate
pytorch_ grad_ Cam -- visual Library of class activation mapping (CAM) under pytorch
TP5 spreadsheet excel table export
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
Getting started with Scala_ Immutable list and variable list
PAT甲级 1020 Tree Traversals
Implementation of window encryption shell
The use and introduction of pytorch 23 hook and the implementation of plug and play dropblock based on hook
清华&华为等 综述 | 语义通信:原则与挑战
Yiwen teaches you Kali information collection
lodash get js代码实现