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 ...
随机推荐
- 服务注册发现consul之二:在Spring Cloud中使用Consul实现服务的注册和发现
首先安装consul环境,参照之前的文章:<服务注册发现consul之一:consul介绍及安装>中的第一节介绍. Spring Cloud使用Consul的服务与发现 1.导入依赖pri ...
- css文字链接滑过向上移动1像素
方法一:行高 a{line-height:22px;} a:hover{line-height:21px;} 方法二:定位 a{position:absolute;top:0;} a:hover{ ...
- WAL基础
WAL(Write-ahead logging,预写式日志)是数据库系统提供原子性和持久化的一系列技术. 在使用WAL的系统中,所有的修改都先被写入到日志中,然后再被应用到系统状态中.通常包含redo ...
- go中的map[Interface{}]Interface{}理解
map里面的k,v支持很多的类型.对于go来说也是,go中有个接口的概念,任何对象都实现了一个空接口.那么我们把map里面的k,v都用interface去定义,当我们在使用这个map的时候,我们可以把 ...
- PHP 多态理解
PHP 多态 多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结果.不同的对象,收到同一消息将可以产生不同的结果,这种现象称为多态性. 多态性允许每个对象以适合自身的方式去响应 ...
- 非ECS阿里云安装插件,给阿里云云监控平台
linux的init学习: https://blog.csdn.net/kunkliu/article/details/80942279 阿里云官方文档: https://help.aliyun.co ...
- [UE4]Return Node节点好用法
蓝图自定义函数中,碰到“Return Node”也会推出当前的函数,跟高级语言的“return”是一样的用法
- Java中关于大小写字母的转换
方法一: 大小写字母在ascii码表中相减的值为32 故可将字符串转车字符数组 小写转大写减32 大写转小写加32 String s= "good"; char[] c = s.t ...
- Windows配置多个git用户
Window配置多个Git账户,SSH连接GitHub.GitLab 最新版本GIt配置对应多个Git仓库(不需要添加多个用户名和邮箱): 在本地git上添加一个用户名和邮箱,生成一对公钥和私钥,把公 ...
- maven的web项目和shiro集成的问题
在自定义shiro然后在加入spring的配置文件时,启动tomcat的时候一直报错 原因:刚学,对maven的依赖构建属性不熟悉 只是完成了maven父项目和子项目的依赖关系,并没有配置子项目之间的 ...