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 ...
随机推荐
- 学习笔记之GitHub
GitHub https://github.com/ GitHub - Wikipedia https://en.wikipedia.org/wiki/GitHub GitHub (originall ...
- C++ Programming Language中的Calculator源代码
C++ Programming Language 4th中的Calculator源代码整理,因为在C++ Programming Language中,涉及了很多文件位置之类的变化,所以,这里只是其中的 ...
- PHPer常见的面试题总结
1.平时喜欢哪些php书籍及博客?CSDN.虎嗅.猎云 2.js闭包是什么? 3.for与foreach哪个更快? 4.php鸟哥是谁?能不能讲一下php执行原理? 5.php加速器有哪些?apc.z ...
- python之路——6
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 增dic['age'] = 21 dic.setfault()删popcleardel popitem ...
- Linux安装MySQL8.0.12之二进制安装
运行环境:centos 7.5 + mysql8.0.12 1.下载官方打包好的二进制安装包: wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysq ...
- xgboost的sklearn接口和原生接口参数详细说明及调参指点
from xgboost import XGBClassifier XGBClassifier(max_depth=3,learning_rate=0.1,n_estimators=100,silen ...
- 微信小程序学习笔记1--小程序的代码构成
最近打算学习一下微信小程序,看了微信公众平台的文档感觉还比较简单,就从这个方向重新找回学习的状态吧: 1.先了解一下小程序的代码构成: 创建项目后会看到四种后缀的文件: .json 后缀的 JSON ...
- vmware workstation14嵌套安装kvm
1.前言 我在2017-11-06使用virtualbox安装了centos,然后嵌套kvm(win7),链接地址如下: https://www.cnblogs.com/tcicy/p/7790956 ...
- linux:ubuntu安装mysql(一)
1.下载MySQL安装包(MySQL Community Server) 地址:https://dev.mysql.com/downloads/mysql/ 2.解压 tar -xvf mysql-s ...
- C# .NET Web Api 保存Session
Global.asax中添加: public override void Init() { this.PostAuthenticateRequest += (sender, e) => Http ...