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.

Have you met this question in a real interview?

Yes
Example

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

Challenge

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 收集雨水的更多相关文章

  1. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  4. 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. [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  ...

  6. [LintCode] Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. *42. Trapping Rain Water 接雨水

    1. 原始题目 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这 ...

  8. 042 Trapping Rain Water 接雨水

    给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...

  9. [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 ...

随机推荐

  1. web前端页面性能优化

    影响用户访问的最大部分是前端的页面.网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发表评论等等.而前端呢?其实应该是属于功能的表现. 而我 ...

  2. Git 常用操作和问题解决

    记录一下自己用git作为项目管理过程中常见的错误以及处理方法 1.git pull 出现问题 git pull出现的问题多为远程分支文件和本地冲突 错误提示:error: Your local cha ...

  3. wpf listview

    <Window x:Class="WpfTutorialSamples.ListView_control.ListViewGridViewSample"        xml ...

  4. 3ds max 渲染清晰面片的边缘

    3ds max的菜单栏 -> 渲染 -> 材质编辑器->精简材质编辑器,将面状打勾,如下图,就能渲染出面片清晰的图形.

  5. Thread比Task多出的无法代替的部分

    Task比Thread耗资源更少,且默认在线程池中. 但是Thread能够设置为STA来执行而Task不能,这对于某些特殊功能很重要,比如WebBrowser控件对象就不能在非单线程单元的线程中new ...

  6. iOS 企业证书的使用文档

    IN-HOUSE应用程序分发 下面介绍下使用网络方式进行部署的方式,用户直接在iPhone/iPad的Safari浏览器里面输入URL地址即可安装, 注意:目前对于这种企业级开发的应用程序最好的分发方 ...

  7. ajaxform使用

    需要引入  jquery.form.js  jquery.js 1.提交的表单 <form id="myForm" action="comment.php" ...

  8. 【体系结构】动态调度算法:记分牌算法和tomasulo算法

    记分牌和tomasulo算法 动态调度: 通过硬件在程序执行时重新安排代码的执行序列来减少竞争引起的流水线停顿时间 动态调度流水线具备以下功能: (1)允许按序取多条指令和发射多条指令----取指(I ...

  9. SharedPreferences 详解

    获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法 2 调用Activity对象的getPreferences()方法 两 ...

  10. maven权威指南学习笔记(四)—— maven生命周期(lifecycle)

    定义: 生命周期是包含在一个项目构建中的一系列有序的阶段 举个例子来说就是maven 对一个工程进行: 验证(validate) -- 编译源码(compile) -- 编译测试源码(test-com ...