LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
代码如下:
class Solution {
public:
int trap(int A[], int n) {
int maxIdx = 0;
int water = 0;
//找到最长的木板,设为maxIdx
for(int i = 1; i < n; i++){
if(A[i] > A[maxIdx]){
maxIdx = i;
}
}
int max = A[0];
//左侧逼近
for(int i = 1; i < maxIdx; i++){
if(max < A[i])
max = A[i];
//木板左边(max)和右边(最高)都比它高,则可以放
// max - 该木板长度 的水
else
water += max - A[i];
}
//右侧逼近
max = A[n - 1];
for(int i = n - 2; i >= maxIdx; i--){
if(max < A[i]) max = A[i];
else water += max - A[i];
}
return water;
}
};
LeetCode 042 Trapping Rain Water的更多相关文章
- [leetcode][042] Trapping Rain Water (Java)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...
- Java for LeetCode 042 Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
随机推荐
- centos6-增加阿里yum源
1.获取阿里的yum源覆盖本地官方yum源 wget -O /etc/yum.repos.d/CentOS-ali.repo http://mirrors.aliyun.com/repo/Centos ...
- 文件流转blob并播放
axios 这里是请求了个mp3做例子: this.$axios({ methods:"GET", url:"/api/music/soures/双笙.mp3" ...
- CCPC 2020 长春站 部分简略题解
gym链接:CCPC 2020 changchun site A: 题目大意:商店里有若干个充值档位和首充奖励,你有\(n\)块钱问最多能拿到多少水. 解:由于档位不多可以直接枚举,整个二进制枚举一下 ...
- 内网渗透 day12-免杀框架2
免杀框架2 目录 1. IPC管道连接 2. 查看wifi密码 3. Phantom-Evasion免杀框架的运用 4. 自解压(sfx) 5. 数字签名 6. 资源替换 1. IPC管道连接 命名管 ...
- 2Git分支问题
1,查看所有分支: git branch *号在哪表明当前分支在哪. 2,新建一个分支: git branch featureq(分支名) 转到该分支下: git checkout featureq ...
- ubuntu 文件编码格式 转换
正在学习jquery,之前在windows下弄的编码到了 ubuntu下,乱码: 找到一个方法: iconv : 源文件:a.htm 格式:gbk: 目标: a.html 格式:utf8: ic ...
- linx 内核 并发与同步 1
内核并发来源: 1.硬件中断和异常:中断服务程序和被中断的进程可能发生并发访问资源 2.软中断和tasklet,软中断和taklet随时都可能倍调度执行,从而打断当前正在执行 进程的上下文. 3.内核 ...
- nginx&http 第三章 ngx http 框架处理流程
1. nginx 连接结构 ngx_connection_t 这个连接表示是客户端主动发起的.Nginx服务器被动接受的TCP连接,我们可以简单称其为被动连接.同时,在有些请求的处理过程中,Nginx ...
- IP 层收发报文简要剖析6--ip_forward 报文转发
//在函数ip_route_input_slow->ip_mkroute_input注册, /* * IP数据包的转发是由ip_forward()处理,该函数在ip_rcv_finish() * ...
- 幻读在 InnoDB 中是被如何解决的?(转)
在MySQL事务初识中,我们了解到不同的事务隔离级别会引发不同的问题,如在 RR 级别下会出现幻读.但如果将存储引擎选为 InnoDB ,在 RR 级别下,幻读的问题就会被解决.在这篇文章中,会先介绍 ...