当前位置:网站首页>Leetcode42. connect rainwater
Leetcode42. connect rainwater
2022-07-05 03:07:00 【what's your name.】
【 difficult 】 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 .
Example 1:
Input :height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output :6
explain : Above is an array of [0,1,0,2,1,0,1,3,2,1,2,1] Height map of representation , under these circumstances , Can connect 6 Units of rain ( The blue part indicates rain ).
Example 2:
Input :height = [4,2,0,3,2,5]
Output :9
Tips :
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
public int trap(int[] height) {
int s = 0;
// Monotonically decreasing stacks Only when the height is satisfied can water be saved , So let's save the index from big to small , Once the current value is greater than the value of the top element , It indicates that the water volume can be settled
Deque<Integer> stack = new LinkedList<>();
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[i] > height[stack.peek()]) {
int right = i;
int mid = stack.pop();
if (!stack.isEmpty()) {
int left = stack.peek();
s += (Math.min(height[left], height[right]) - height[mid]) * (right - left - 1);
}
}
stack.push(i);
}
return s;
}
边栏推荐
- SFTP cannot connect to the server # yyds dry goods inventory #
- Comparison of advantages and disadvantages between platform entry and independent deployment
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
- Linux安装Redis
- Hmi-32- [motion mode] add light panel and basic information column
- Dart series: collection of best practices
- Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it
- D3js notes
- Azkaban安装部署
猜你喜欢

Three line by line explanations of the source code of anchor free series network yolox (a total of ten articles, which are guaranteed to be explained line by line. After reading it, you can change the

Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time

Bumblebee: build, deliver, and run ebpf programs smoothly like silk

Ubantu disk expansion (VMware)

腾讯云,实现图片上传

Kbp206-asemi rectifier bridge kbp206
![Hmi-31- [motion mode] solve the problem of picture display of music module](/img/9c/0b25c0a41758652848aed2a269880f.jpg)
Hmi-31- [motion mode] solve the problem of picture display of music module

【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)

2.常见的请求方法

Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
随机推荐
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
【微服务|SCG】Filters的33种用法
Utilisation simple de devtools
Cut! 39 year old Ali P9, saved 150million
D3js notes
Sqoop安装
SFTP cannot connect to the server # yyds dry goods inventory #
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Avoid material "minefields"! Play with super high conversion rate
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
Acwing第 58 场周赛【完结】
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
Linux Installation redis
GFS distributed file system
Moco V2 literature research [self supervised learning]
Why is this an undefined behavior- Why is this an undefined behavior?
为什么腾讯阿里等互联网大厂诞生的好产品越来越少?
Azkaban安装部署
Vb+access hotel service management system
Pdf things