【Lintcode】363.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.
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
题解:
读题可知,区间[a,b]内容积由边缘最小值决定,Two pointer思想,遍历整个数组。每一次都是寻找两端最小值,然后开始遍历,遇到更大值后替换端值(不能盛水了)。
Solution 1 () (from here 九章算法)
class Solution {
public:
int trapRainWater(vector<int> &heights) {
int left = , right = heights.size() - ;
int res = ;
if (left >= right) {
return res;
}
int lheight = heights[left];
int rheight = heights[right];
while (left < right) {
if (lheight < rheight) {
++left;
if (lheight > heights[left]) {
res += lheight - heights[left];
} else {
lheight = heights[left];
}
} else {
--right;
if (rheight > heights[right]) {
res += rheight - heights[right];
} else {
rheight = heights[right];
}
}
}
return res;
}
};
其他解法见此处
【Lintcode】363.Trapping Rain Water的更多相关文章
- 【Lintcode】364.Trapping Rain Water II
题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell i ...
- 【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
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 【一天一道LeetCode】#42. Trapping Rain Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode题意分析&解答】42. 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 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
随机推荐
- python 基础 9.7 创建表
一. 创建表 #/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/22 18:05 #@Auther :liuzhenchuan #@Fi ...
- 线性同余方程模板( A+C*x=B(mod D) )
void extendgcd(long long a,long long b,long long &d,long long &x,long long &y) { ){d=a;x ...
- 1 了解Scala
1 定义变量 单个变量:var name = "benxintuzi" 等价于 var name : String = "benxintuzi"(即定义变量时 ...
- is assembler instruction and machine instuction atomic
1 assembler instruction depends,有的汇编指令会被assemble成多条机器指令. 2 机器指令 depends,有的机器指令也不是atomic的. 所以,不要希望在单条 ...
- iphone传感器
传感器 什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都是由传感 ...
- SQLite支持的并发访问数
SQLite objects created in a thread can only be used in that same thread.The object was created in th ...
- 移动端meta viewport
<meta name="viewport" content=" width=device-width, user-scalable=no, initial-scal ...
- linux c编程:线程退出
在线程创建的时候pthread_exit都是调用的固定参数,我们先来看下如果用自动变量作为pthread_exit的参数时出现的问题 typedef struct foo{ int a; int b; ...
- spring项目改名后不能启动的原因及解决办法
今日修改了一个spring项目的项目名称,修改后启动项目Debug as->Debug on server,过了很久也没有出现web首页,仔细看项目的定时器已经启动,eclipse的Consol ...
- CentOS7 添加用户到 sudoers
1.在装完系统后,CentOS默认普通用户是无法使用sudo命令的,这时我们就需要将登陆的用户加入/etc/sudoers 文件中 假设用户名是yhd 3.切换到超级用户:$ su - 当然输入的 p ...