当前位置:网站首页>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;
}
边栏推荐
- Design of kindergarten real-time monitoring and control system
- Share the newly released web application development framework based on blazor Technology
- Character painting, I use characters to draw a Bing Dwen Dwen
- 看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
- 【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
- 1.五层网络模型
- GFS分布式文件系统
- Scientific research: are women better than men?
- 有个疑问 flink sql cdc 的话可以设置并行度么, 并行度大于1会有顺序问题吧?
- Design and implementation of kindergarten management system
猜你喜欢

2021 Li Hongyi machine learning (3): what if neural network training fails

Azkaban实战

8. Commodity management - commodity classification

IPv6 experiment

Sqoop安装

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics

Pat grade a 1119 pre- and post order traversals (30 points)

Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
随机推荐
Design and practice of kubernetes cluster and application monitoring scheme
Dart series: collection of best practices
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
Spark SQL learning bullet 2
【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
[micro service SCG] 33 usages of filters
Talk about the SQL server version of DTM sub transaction barrier function
1.五层网络模型
Character painting, I use characters to draw a Bing Dwen Dwen
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Flume配置4——自定义MYSQLSource
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
Accuracy problem and solution of BigDecimal
【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)
LeetCode --- 1071. Great common divisor of strings problem solving Report
Tiny series rendering tutorial
Avoid material "minefields"! Play with super high conversion rate
Azkaban实战
2021 Li Hongyi machine learning (1): basic concepts