当前位置:网站首页>28. Rainwater connection
28. Rainwater connection
2022-07-24 12:46:00 【Little happy】
42. Rainwater collection
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
public int trap(int[] height) {
int sum = 0;
// You don't have to think about the columns at the end of the line , Because there must be no water . So the subscript is from 1 To length - 2
for (int i = 1; i < height.length - 1; i++) {
int max_left = 0;
// Find the highest on the left
for (int j = i - 1; j >= 0; j--) {
if (height[j] > max_left) {
max_left = height[j];
}
}
int max_right = 0;
// Find the highest on the right
for (int j = i + 1; j < height.length; j++) {
if (height[j] > max_right) {
max_right = height[j];
}
}
// Find the smaller end of
int min = Math.min(max_left, max_right);
// Only the smaller segment is greater than the height of the current column will have water , There will be no water in other cases
if (min > height[i]) {
sum = sum + (min - height[i]);
}
}
return sum;
}
Monotonic stack
public int trap6(int[] height) {
int sum = 0;
Stack<Integer> stack = new Stack<>();
int current = 0;
while (current < height.length) {
// If the stack is not empty and the current height is greater than the height of the top of the stack, the cycle will continue
while (!stack.empty() && height[current] > height[stack.peek()]) {
int h = height[stack.peek()]; // Take out the element to be out of the stack
stack.pop(); // Out of the stack
if (stack.empty()) {
// Go out when the stack is empty
break;
}
int distance = current - stack.peek() - 1; // The distance between the two walls .
int min = Math.min(height[stack.peek()], height[current]);
sum = sum + distance * (min - h);
}
stack.push(current); // Stack the currently pointed wall
current++; // Pointer backward
}
return sum;
}
边栏推荐
- Seckill implementation diagram
- Vscode solves the problem of terminal Chinese garbled code
- 让一套代码完美适配各种屏幕
- English grammar_ Indefinite pronouns - Overview
- Qt Creator怎样更改默认构建目录
- 树莓派自建 NAS 云盘之——树莓派搭建网络存储盘
- The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
- Say no to blackmail virus, it's time to reshape data protection strategy
- Compatibility problems of call, apply, bind and bind
- Native Crash的一切
猜你喜欢

SSM在线校园相册管理平台

English grammar_ Indefinite pronouns - Overview

SSM online rental and sales platform multi city version
This is how the permission system is designed, yyds

ThinkPHP realizes database backup

国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确

English语法_不定代词 - 概述
![Error: [synth 8-439] module 'xxx' not found not found error solution](/img/47/bb03cc26e254332bf815c80bafb243.png)
Error: [synth 8-439] module 'xxx' not found not found error solution

The price of domestic flagship mobile phones is nearly 6000, but they can't even beat iphone12. It's clear who users choose

从零实现深度学习框架——再探多层双向RNN的实现
随机推荐
Is there any entrepreneurship project suitable for one person in the early stage of 2W and 3W? Is it OK to be we media?
Opencv:08 image pyramid
Where+or usage of SQL missing condition
【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
Support liuhaiping
手把手教你用 Power BI 实现 4 种可视化图表
It is difficult for Chinese consumers and industrial chains to leave apple, and iPhone has too much influence
Nacos deployment
[function test] test of the project - login and post function
More functions and functions of the metauniverse lie in the deep transformation of the traditional way of life and production
猿人学第五题
1.4.1 many, many symbols (cin/cout unbinding, O3 optimization)
Zabbix5.0.8-odbc monitoring Oracle11g
Node takes effect after using NVM to install under Windows system, but NPM does not take effect
Reserved instances & Savings Plans
Implement is by yourself_ default_ constructible
猿人学第六题
Prototype inheritance
The seventh topic of ape Anthropology
Error: [synth 8-439] module 'xxx' not found not found error solution