Leetcode 题解 Trapping Rain Water
题目链接:https://leetcode.com/problems/trapping-rain-water/description/
思路:
1、先找到最左侧第一个高度不为0的柱子i。
2、从i+1开始向右侧找另一个柱子max。条件:要么a[max] > a[i],要么a[max] > 右侧所有。
1) 向右侧遍历,当遇到第一个比a[i]高的柱子,相当于遇到一座山,这个柱子会把左右两侧的池子给隔开。
所以,如果遇到第一个比a[i]高的柱子,就停下来。这时候就产生了一个和右侧隔开的池子。
2) 如果找到最后,一直没有遇到比a[i]还高的,那就取右侧最高的那个作为max。
a[max] 和 a[i]组成一个和右侧隔开的池子。
池子的面积就是 min(a[max] , a[i]) * (max - i - 1) - sum(a[i+1]...a[max-1])
3、把i定位到max,重复上述过程。以max为起点,找下一个池子。
show me the code:
class Solution {
public:
int trap(vector<int>& height) {
vector<int> &a = height;
int i,n = a.size();
int sum = ;
for(i = ;i < n - ; )
{
while(i < n - && a[i] == ) ++i;
int max = i + ; //i右侧最大值的下标
for(int j = i+; j < n; j++)
{
if(a[j] > a[max]) max = j;
if(a[max] > a[i]) break;
}
sum += min(a[i],a[max])*(max - i - );
for(int j = i+; j< max; j++)
sum -= a[j];
i = max;
}
return sum;
}
};
Leetcode 题解 Trapping Rain Water的更多相关文章
- 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 ------------------------------------------------------------- ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
随机推荐
- get和post的解析
get 直接获取数据 post 将数据发送到服务端缓存下来,当无法缓存数据或用户输入未知字符时用post, get:常用于数据 post:不想缓存数据,不再浏览器中缓存数据,在服务端可以,因为取数据及 ...
- python urlib2报错gaierror: [Errno 11004] getaddrinfo failed
gaierror : get address info error,获取网络地址信息错误. url不正确,代理信息配置不正确都会报这个错误. 摘自https://blog.csdn.net/qq_19 ...
- tesseract-ocr4.0 安装部署及训练验证码识别
1. 下载最新版本的leptonica, leptonica-1.74.1.tar.gz 2. 编译安装 tar -zxvf leptonica-.tar.gz cd leptonica- . ...
- VMware虚拟机上配置nginx后,本机无法访问问题(转载)
转自:http://www.server110.com/nginx/201407/10794.html 把nginx装在CentOS上,用本机访问虚拟机的时候却出现了不能访问的问题,查了资料以后,原来 ...
- UI 性能因素考虑
浏览器的最大并发连接数一般在4到6之间,首先了解影响加载的性能因素: (1)下载的文件太大 (2)发出的请求太多 (3)请求相应不及时 针对这些因素,一般会考虑减少请求次数: (1)对静态文件设置缓存 ...
- 第6章 静态路由和动态路由(4)_OSPF动态路由协议
6. OSPF动态路由协议 6.1 OSPF协议(Open Shortest Path First,OSPF开放式最短路径优先协议) (1)通过路由器之间通告链路的状态来建立链路状态数据库,网络中所有 ...
- 关于HSTS
HSTS(HTTP Strict Transport Security) 当用户在浏览器中输入一个域名,如果没有注明前缀(也就是没输入"http"的时候)的时候,浏览器会默认按照h ...
- 踩过的坑:InteliIJ IDEA 打开的项目突然左侧目录结构消失了,如何处理?
试了很多的办法,删除项目,然后重新从git下载,再导入项目,但是对于暂存未上传到git的文件也会被一并删除,这样就亏大发了 之前一直没有找到解决办法,这里记一下终身有效的办法,并且比较好操作 按下列步 ...
- 2013年第四届蓝桥杯JavaB组省赛试题解析
题目及解析如下: 题目大致介绍: 第一题到第四题是结果填空,方法不限只要得到最后结果就行 第五题和第六题是代码填空题,主要考察算法基本功和编程基本功 第七题到第十题是编程题,要求编程解决问题 第一题 ...
- vue2.0 slot用法
学习vue.js也有一段时间了,关于slot这一块,也看了不少次了,总感觉有点迷迷糊糊,不知其然也不知其所以然,抽出一段完整的时间,再一次仔细学习.稍微有点理解了,在此稍作记录,好记性不如烂笔头嘛! ...