当前位置:网站首页>[array]bm94 rainwater connection problem - difficult
[array]bm94 rainwater connection problem - difficult
2022-06-27 03:49:00 【51CTO】
Knowledge point Double pointer Monotonic stack Dynamic programming
describe
Given an array of integers arr, It is known that all values are nonnegative , Think of this array as a column height map , Calculate columns arranged in this way , How much rain can be received after rain .( The height of the area outside the array is treated as 0)![[ Array ]BM94 The rain problem - More difficult _ Double pointer](/img/2b/1934803060d65ea9139ec489a2c5f5.png)
Data range : The length of the array , Each value in the array satisfies
, Ensure that the returned results meet
requirement : Time complexity
Example 1
Input :
Copy the return value :
Copy instructions :
Example 2
Input :
Copy the return value :
Answer key
Auxiliary array solution
Ideas :
Let's say the array is a, For any subscript i, Its capacity to hold water depends on the highest water on its left and right sides 2 The one with the lowest root , Suppose the height is h, The capacity for holding water is h - a[i]. We just find out for any of the following tables i, It is the highest around
Steps are as follows :
- The trial 2 An array max_left[i] and max_right[i] Represent the subscript respectively i When , On the left [0,i-1] The highest pillar , And from right to left [n-1,i+1] The highest pillar , Traversal array , First solve these two arrays
- Traversal array again , When the subscript is reached i When , The capacity of this column to hold water is v = min(max_left[i],max_right[i]) - arr[i]
- Accumulate the second step to get the answer
The code is as follows :
// 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.
Double pointer solution
In the above solution, we use 2 An auxiliary array to hold a subscript i Maximum left and right height of . So can we know the subscript without using the auxiliary array i The maximum left and right height of ? In fact, it's easy to know when you think about it , It's easy to write an answer with two pointers .
step :
- Use 2 A variable left_hight and right_hight The maximum height of the leftmost and rightmost code respectively
- Use 2 A pointer to the i and k Respectively from the 0 and n-1 Start traversing at the location of
- If a[i] < left_hgith && a[i] < right_hight, Then the index is i The location of the can accommodate min(left_hight,right_hight)-a[i] Of water
- If a[i] >= left_hight && a[i] <= right_hight, take i Move right
- If a[i] >= left_hight && a[i] >= right_hight, take k Move left
long
long
maxWater(
const
vector
<
int
>
&
arr)
{
if (
arr.
size()
<=
2)
{
return
0;
}
// Left and right hands
int
left
=
0;
int
right
=
arr.
size()
-
1;
// stay [left,right] It is the highest on the left outside the section 、 The highest on the right
int
left_max
=
0;
int
right_max
=
0;
int
res
=
0;
while (
left
<=
right)
// Traverse the entire array
{
// Get the height of the short side
int
min_hight
=
std::min(
left_max,
right_max);
res
+=
std::max(
0,
min_hight
-
std::min(
arr[
left],
arr[
right]));
// Accumulate the capacity that the current index can hold
left_max
=
std::max(
left_max,
arr[
left]);
// Update left height
right_max
=
std::max(
right_max,
arr[
right]);
// Update right height
if (
arr[
left]
<
arr[
right])
// Update index , Move the shorter side towards the middle
{
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.
边栏推荐
- MobileNet系列(4):MobileNetv3网络详解
- Overview of Tsinghua & Huawei | semantic communication: Principles and challenges
- Fplan powerplan instance
- Kotlin compose implicitly passes the parameter compositionlocalprovider
- nignx配置单ip限流
- 乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。
- 2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
- Window 加密壳实现
- 实践 DevOps 时,可能面临的六大挑战
- FastDDS的服务器记录-译-
猜你喜欢
![[promise I] introduction of promise and key issues of hand rolling](/img/14/94bd986d3ac8a0db35c83b4234fa8a.png)
[promise I] introduction of promise and key issues of hand rolling

2021:Greedy Gradient Ensemble for Robust Visual Question Answering

2021:adavqa: overlapping language priors with adapted margin cosine loss *

PAT甲级 1026 Table Tennis

快速掌握 ASP.NET 身份认证框架 Identity - 通过邮件重置密码

Pat grade a 1020 tree Traversals

WPF open source control library extended WPF toolkit Introduction (Classic)

静态时序分析-OCV和time derate

为什么 C# 访问 null 字段会抛异常?

Why does C throw exceptions when accessing null fields?
随机推荐
Games101 job 7 improvement - implementation process of micro surface material
Quickly master asp Net authentication framework identity - reset password by mail
2022 Chinese pastry (Advanced) recurrent training question bank and online simulation test
Pat grade a 1018 public bike management
再探Handler(上)(Handler核心原理最全解析)
WPF 开源控件库Extended WPF Toolkit介绍(经典)
卷积神经网络(CNN)网络结构及模型原理介绍
电商产品如何在知乎上进行推广和打广告?
文旅夜游|以沉浸式视觉体验激发游客的热情
Pat grade a 1020 tree Traversals
Ledrui ldr6035 usb-c interface device supports rechargeable OTG data transmission scheme.
PAT甲级 1020 Tree Traversals
Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
ESP8266
手机新领域用法知识
ERP需求和销售管理 金蝶
Agile development - self use
2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
2021:Greedy Gradient Ensemble for Robust Visual Question Answering
Logarithm