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 ...
随机推荐
- VI使用手册(常见命令)
VI使用手册 模式切换 i键开始进入编辑模式,Esc进入一般模式,保存退出:wq,不保存退出q,强制退出q! 如何定位到行文档首位,行首位? gg或者1G命令将光标移动到文档开头G命令将光标移动到文档 ...
- mysql实现自增函数
这两天在思考怎么生成数据库随机名称,思前想后觉得还是利用自增的逻辑主键是最方便快捷的,于是便尝试着获取一种自增的mysql函数 自增mysql函数 BEGIN DECLARE id INT DEFAU ...
- cas server
Tomcat: V8.5.x Java: 1.8 x64 MySQL: 5.5.x OS: Win10 x64 I. war 0. clone git clone https://github.com ...
- HBase数据模型和读写原理
Hbase的数据模型和读写原理: HBase是一个开源可伸缩的分布式数据库,他根据Google Bigtable数据模型构建在hadoop的hdfs存储系统之上. HBase是一个稀疏.多维度 ...
- flask表单标签
环境:python2.7+flask+flask_wtf 内容:flask的部分常用表单标签 test.py # -*- coding: utf-8 -*- # flask web_form 表单控件 ...
- Android模块化开发、组件化开发;
模块化开发:优点嘛,项目过大时便于管理: 1.在根目录的gradle.properties文件下添加 isBuildModule=false: 使用isBuildModule来控制这个是Library ...
- python连接RabbitMQ
安装PIP wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz tar -xzvf pip-.tar.g ...
- git解决冲突插件之Beyond Compare
Beyond Compare主要作用: 1. 可以比较文件.文件夹的差异: 2. 将一个文件或文件夹的两个不同版本进行变更合并,生成一个输出. 基于以上两个特性,可以将beyond compare集成 ...
- 【转】oracle定制定时执行任务
本节摘要:本节介绍使用oracle自带的job来实现oracle定制定时执行任务. 1.引言 定制定时执行的任务有两种形式,系统级别和数据库级别, 从操作系统级别来讲, windows系统我们可以使用 ...
- Error building Player: UnityException: Bundle Identifier has not been set up correctly
错误提示: Error building Player: UnityException: Bundle Identifier has not been set up correctlyPlease s ...