当前位置:网站首页>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;
}
边栏推荐
- Azkaban actual combat
- Talk about the SQL server version of DTM sub transaction barrier function
- ELK日志分析系统
- Design and implementation of high availability website architecture
- Kbp206-asemi rectifier bridge kbp206
- Use of kubesphere configuration set (configmap)
- VM in-depth learning (XXV) -class file overview
- 端口,域名,协议。
- Design of KTV intelligent dimming system based on MCU
- Pdf things
猜你喜欢
ELK日志分析系统
Cut! 39 year old Ali P9, saved 150million
El select, El option drop-down selection box
Flume配置4——自定义MYSQLSource
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
Design and implementation of community hospital information system
Master Fur
SQL performance optimization skills
2021 Li Hongyi machine learning (1): basic concepts
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
随机推荐
【微服务|SCG】Filters的33种用法
Breaking the information cocoon - my method of actively obtaining information - 3
8. Commodity management - commodity classification
Moco V2 literature research [self supervised learning]
Dart series: collection of best practices
Design and implementation of high availability website architecture
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Sqoop installation
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
2022/02/13
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Flume configuration 4 - customize mysqlsource
IPv6 experiment
SQL injection exercise -- sqli Labs
Kbp206-asemi rectifier bridge kbp206
Pytest (4) - test case execution sequence
The latest blind box mall, which has been repaired very popular these days, has complete open source operation source code
Master Fur
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience