【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 ...
随机推荐
- mysql如何监测是否命中索引?
使用执行计划. 什么是执行计划? EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARN ...
- hdu3579(线性同余方程组)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- gulp安装教程
1.安装nodejs并选装cnpm: npm install cnpm -g --registry=https://registry.npm.taobao.org 2.全局安装gulp: cnpm i ...
- BI测试
BI概念: 商业智能(Business Intelligence 简称BI),指数据仓库相关技术与应用的通称.指利用各种智能技术,来提升企业的商业竞争力.是帮助企业更好地利用数据提高决策质量的技术,包 ...
- linux服务器最大连接数
1 受内存限制 每个tcp连接是一个打开的socket文件,因此linux服务器的最大连接数受linux操作系统单个进程同时打开的最大文件数的限制. 这个限制本质上是对单个进程内存的限制. 查看进程最 ...
- 特性,批次特性建立的BAPI函數
[转http://taijizhang.blog.163.com/blog/static/176071381201442225514453/] SAP特性,物料特性,批次特性建立的BAPI函數 类的T ...
- 初识机器学习之kNN算法
k近邻(k-Nearest Neighbor,简称kNN)学习是一种常用的监督学习方法,其工作机制非常简单:给定测试样本,基于某种距离度量找出训练集中与其最靠近的k个训练样本.然后基于这k个“邻居”的 ...
- Python基础(5)_字符编码、文件处理
一.文件读取过程: 1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编写的内容也都是存放与内存中的,断电后数 ...
- JDK线程池的实现
线程池 接口Executor 该接口只有一个方法,JDK解释如下 执行已提交的Runnable 任务的对象.此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节.调度等)分离开来的方 ...
- Python——轻量级web服务器flask的学习
前言: 根据工程需要,开始上手另一个python服务器---flask,flask是一个轻量级的python服务器,简单易用.将我的学习过程记录下来,有新的知识会及时补充. 记录只为更好的分享~ 正文 ...