当前位置:网站首页>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;
}
边栏推荐
- Why does 2.tostring() report an error
- Implementing deep learning framework from zero -- further exploration of the implementation of multilayer bidirectional RNN
- Buckle practice - 30 set the intersection size to at least 2
- 2022.07.15 暑假集训 个人排位赛(十)
- 使用TypeFace设置TextView的文字字体
- SQL JOIN 入门使用示例学习左连接、内连接、自连接
- 权限系统就该这么设计,yyds
- The basis of point graph in the map of life information and knowledge
- 1.9. touch pad test
- Correct use of qwaitcondition
猜你喜欢

【C语言】详细的文件操作相关知识

English语法_不定代词 - 概述

Solutions to problems in IE6 browser

nacos部署

基于Qt的软件框架设计

Getting started with SQL join use examples to learn left connection, inner connection and self connection

Cluster construction based on kubernetes v1.24.0 (III)

Cluster construction based on kubernetes v1.24.0 (I)
让一套代码完美适配各种屏幕
![[function test] test of the project - login and post function](/img/64/c9bbf34964622f4f013b1184eeb1e0.jpg)
[function test] test of the project - login and post function
随机推荐
Say no to blackmail virus, it's time to reshape data protection strategy
突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战
Buckle practice - sum of 34 combinations
Zabbix5.0.8-odbc monitoring Oracle11g
Is it safe to open an account on Oriental Fortune online? Is there a threshold for opening an account?
C language course design -- hotel management system
我在一个模块工程中使用注解配置了redis的序列化, 然后在另外一个模块引入这个模块,为什么这个配置
基于Kubernetes v1.24.0的集群搭建(二)
Buckle exercise - 35 combination sum II
STM32 - Fundamentals of C language
Cluster construction based on kubernetes v1.24.0 (II)
【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
ERROR: [Synth 8-439] module ‘xxx‘ not found not found 错误解决办法
Unity rotation test
Try... Finally summary
有没有2、3w前期适合一个人干的创业项目呢?做自媒体可以吗?
class
Opencv:08 image pyramid
中国消费者和产业链都很难离开苹果,iPhone的影响力太大了
以Chef和Ansible为例快速入门服务器配置