[LintCode] 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
.
O(n) time and O(1) memory
O(n) time and O(n) memory is also acceptable.
LeetCode上的原题,请参见我之前的博客Trapping Rain Water。
解法一:
class Solution {
public:
/**
* @param heights: a vector of integers
* @return: a integer
*/
int trapRainWater(vector<int> &heights) {
int res = , mx = , n = heights.size();
vector<int> dp(n, );
for (int i = ; i < n; ++i) {
dp[i] = mx;
mx = max(mx, heights[i]);
}
mx = ;
for (int i = n - ; i >= ; --i) {
dp[i] = min(dp[i], mx);
mx = max(mx, heights[i]);
if (dp[i] > heights[i]) res += dp[i] - heights[i];
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param heights: a vector of integers
* @return: a integer
*/
int trapRainWater(vector<int> &heights) {
int res = , l = , r = heights.size() - ;
while (l < r) {
int mn = min(heights[l], heights[r]);
if (mn == heights[l]) {
++l;
while (l < r && heights[l] < mn) {
res += mn - heights[l++];
}
} else {
--r;
while (l < r && heights[r] < mn) {
res += mn - heights[r--];
}
}
}
return res;
}
};
解法三:
class Solution {
public:
/**
* @param heights: a vector of integers
* @return: a integer
*/
int trapRainWater(vector<int> &heights) {
int res = , l = , r = heights.size() - , level = ;
while (l < r) {
int lower = heights[(heights[l] < heights[r]) ? l++ : r--];
level = max(level, lower);
res += level - lower;
}
return res;
}
};
[LintCode] Trapping Rain Water 收集雨水的更多相关文章
- [LeetCode] 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 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping rain water II
Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 ...
- [LintCode] Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- *42. Trapping Rain Water 接雨水
1. 原始题目 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这 ...
- 042 Trapping Rain Water 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...
- [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 ...
随机推荐
- js:使用js过程中遇到的一个小问题
在一个作业中使用了js,函数A调用函数B.当A和B中均含有变量i的时候,相关操作结果可能会出错. 将B中的i替换为j(j不存在于A中)后,结果正确. 目前考虑原因是两个变量i有相关性(或者说实际上就是 ...
- samba共享目录
samba 原理:在linux机器上共享一个目录出来,让windows通过网上邻居去访问 (i)共享一个不需要输入用户名和密码就能访问的目录(可读不可写) 一.打开配置文件: vim /etc/sam ...
- 使用JSOM检查文件或文件夹是否存在
How to Check with SharePoint JSOM if File or Folder Exists Here's a code snippet showing how to use ...
- myeclipse实现包的分层显示
- Buffer类
输入流中可以通过缓冲区来加大读取的效率,sun公司感觉可以加快执行效率,他就为我们提供了一个类来操作缓存区. Buffer来头的类:所有缓冲流都是以Buffer开头的: 学习缓冲流的作用: Buffe ...
- 从SQLite获取数据完成一个产品信息展示
在ios实际开发当中,我们常常用到Core Data做为数据储存首选.但在处理一些大量复杂的数据值且数据之间相互关联的时候,这就不得不使用关系型数据库来实现.例如一个导航程序,自身应该包含大量的地图自 ...
- js加解密字符串
项目中经常会有url带参并取参数值的问题,最常见的就是登录后再回到原来的页面redirect_uri,我们通常会使用 encodeURIComponent() 转码,当然简单的可以这样使用,如果涉及到 ...
- 与你相遇好幸运,制作自己的Yeoman Generator
使用别人写好的生成器: npm install -g yonpm install -g generator-angularyo angular 如何自己制作符合自己心仪的生成器呢: https://g ...
- python3 与 pip3 安装与使用
1. yum -y install openssl* (pip依赖ssl环境) 2.编译安装python3 下载地址:https://www.python.org/ftp/python/ .tgz c ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...