当前位置:网站首页>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;
}
边栏推荐
- Accuracy problem and solution of BigDecimal
- Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
- D3js notes
- Share the newly released web application development framework based on blazor Technology
- Pat grade a 1119 pre- and post order traversals (30 points)
- Design and practice of kubernetes cluster and application monitoring scheme
- Share the newly released web application development framework based on blazor Technology
- GFS分布式文件系统
- 8. Commodity management - commodity classification
- Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
猜你喜欢
Pat class a 1162 postfix expression
This + closure + scope interview question
el-select,el-option下拉选择框
Talk about the SQL server version of DTM sub transaction barrier function
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
Asp+access campus network goods trading platform
Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
2021 Li Hongyi machine learning (2): pytorch
SQL performance optimization skills
随机推荐
单项框 复选框
Why is this an undefined behavior- Why is this an undefined behavior?
Sqoop installation
SQL injection exercise -- sqli Labs
LeetCode 237. Delete nodes in the linked list
[105] Baidu brain map - Online mind mapping tool
看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
Comparison of advantages and disadvantages between platform entry and independent deployment
Kbp206-asemi rectifier bridge kbp206
Sqoop command
[200 opencv routines] 99 Modified alpha mean filter
数据库和充值都没有了
Asp+access campus network goods trading platform
Sqoop安装
Pat class a 1160 forever (class B 1104 forever)
Pdf things
Utilisation simple de devtools
2021 Li Hongyi machine learning (2): pytorch
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety