当前位置:网站首页>Leetcode 42. rainwater connection
Leetcode 42. rainwater connection
2022-07-26 06:09:00 【LuZhouShiLi】
Leetcode 42. Rainwater collection
subject
Given n Nonnegative integers indicate that each width is 1 Height map of columns of , Calculate columns arranged in this way , How much rain can be received after rain .
Ideas
Double pointer solution
Traverse all columns from the beginning , Except for the first element and the last element , For each element, calculate the highest element on the left and the highest element on the right of the column , And take the minimum , Subtract the height of the current column from this minimum , Is the height of rainwater that can be accumulated in this column .Dynamic programming
- The highest left height of the current position is the highest left height of the previous position and the maximum value of the current height
- The highest height on the right of the current position is the highest height on the right of the previous position and the maximum value of the current height
Code
- Double pointer solution
class Solution {
public:
int trap(vector<int>& height) {
int sum = 0;
for(int i = 0; i < height.size(); i++)
{
// The first pillar and the last pillar don't receive rain
if(i == 0 || i == height.size() - 1)
{
continue;
}
// Record the highest columns on the left and right
int rheight = height[i];
int lheight = height[i];
for(int r = i + 1; r < height.size(); r++)
{
if(height[r] > rheight)
{
rheight = height[r];// Update maximum column
}
}
for(int l = i - 1; l >= 0; l--)
{
if(height[l] > lheight)
{
lheight = height[l];
}
}
int h = min(lheight,rheight) - height[i];
if(h > 0)
{
sum += h;
}
}
return sum;
}
};
- Dynamic programming
class Solution {
public:
int trap(vector<int>& height) {
if(height.size() <= 2)
{
return 0;
}
vector<int> maxLeft(height.size(),0);
vector<int> maxRight(height.size(),0);
int size = maxRight.size();
// Record the maximum height of the left column of each column
maxLeft[0] = height[0];
for(int i = 1; i < size; i++)
{
// The maximum height on the left of the current position is the maximum value of the lag between the maximum height on the left of the previous position and this height
maxLeft[i] = max(height[i],maxLeft[i - 1]);
}
// Record the maximum height of the right column of each column
maxRight[size - 1] = height[size - 1];// First initialize to the height of the last column
// Then push from the penultimate column
for(int i = size - 2; i >= 0; i--)
{
maxRight[i] = max(height[i],maxRight[i + 1]);
}
// Sum up
int sum = 0;
for(int i = 0; i < size;i++)
{
int count = min(maxRight[i],maxLeft[i]) - height[i];
if(count > 0)
{
sum += count;
}
}
return sum;
}
};
边栏推荐
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- Mobile web
- Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
- 2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
- 金仓数据库 KingbaseES SQL 语言参考手册 (5. 操作符)
- Leetcode:336. palindrome pair
- Kingbasees SQL language reference manual of Jincang database (8. Function (10))
- 【Day_03 0420】数组中出现次数超过一半的数字
- Read five articles in the evening | Economic Daily: don't treat digital collections as wealth making products
- 英语句式参考纯享版 - 定语从句
猜你喜欢

“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树

某公司给每个工位装监控:只为看员工写代码?

Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib

A company installs monitoring for each station: write code only to see employees?

Matlab vector and matrix

Convolutional neural network (III) - target detection

VRRP principle and basic commands

Interview difficulties: difficulties in implementing distributed session, this is enough!

Understanding the mathematical essence of machine learning

PHP 多任务秒级定时器的实现方法
随机推荐
【2023杰理科技提前批笔试题】~ 题目及参考答案
Mysql45 talking about global lock, table lock and row lock
Xiao He shows his sharp corners and says hello to flutter app
Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
Intelligent fire protection application based on fire GIS system
Understanding the mathematical essence of machine learning
em和rem
Distributed | practice: smoothly migrate business from MYCAT to dble
Calling mode and execution sequence of JS
某公司给每个工位装监控:只为看员工写代码?
K. Link with Bracket Sequence I dp
Webapi collation
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
Taobao JD pinduoduo Tiktok taote 1688 and other multi platform commodity app details API interfaces (commodity details page data interface, commodity sales interface, keyword search commodity sales in
【Day05_0422】C语言选择题
Latex同时合并表格的多行多列
VS中使用动态库
时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
Flex layout
YOLOv6:又快又准的目标检测框架开源啦