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.html
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.算法
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!数组
这道收集雨水的题跟以前的那道 Largest Rectangle in Histogram 直方图中最大的矩形 有些相似,可是又不太同样,咱们先来看一种方法,这种方法是基于动态规划Dynamic Programming的,咱们维护一个一维的dp数组,这个DP算法须要遍历两遍数组,第一遍遍历dp[i]中存入i位置左边的最大值,而后开始第二遍遍历数组,第二次遍历时找右边最大值,而后和左边最大值比较取其中的较小值,而后跟当前值A[i]相比,若是大于当前值,则将差值存入结果,代码以下:app
C++ 解法一:post
class Solution { public: int trap(vector<int>& height) { int res = 0, mx = 0, n = height.size(); vector<int> dp(n, 0); for (int i = 0; i < n; ++i) { dp[i] = mx; mx = max(mx, height[i]); } mx = 0; for (int i = n - 1; i >= 0; --i) { dp[i] = min(dp[i], mx); mx = max(mx, height[i]); if (dp[i] > height[i]) res += dp[i] - height[i]; } return res; } };
Java 解法一:优化
public class Solution { public int trap(int[] height) { int res = 0, mx = 0, n = height.length; int[] dp = new int[n]; for (int i = 0; i < n; ++i) { dp[i] = mx; mx = Math.max(mx, height[i]); } mx = 0; for (int i = n - 1; i >= 0; --i) { dp[i] = Math.min(dp[i], mx); mx = Math.max(mx, height[i]); if (dp[i] - height[i] > 0) res += dp[i] - height[i]; } return res; } }
最后咱们来看一种只须要遍历一次便可的解法,这个算法须要left和right两个指针分别指向数组的首尾位置,从两边向中间扫描,在当前两指针肯定的范围内,先比较两头找出较小值,若是较小值是left指向的值,则从左向右扫描,若是较小值是right指向的值,则从右向左扫描,若遇到的值比当较小值小,则将差值存入结果,如遇到的值大,则从新肯定新的窗口范围,以此类推直至left和right指针重合,具体参见代码以下:this
C++ 解法二:url
class Solution { public: int trap(vector<int>& height) { int res = 0, l = 0, r = height.size() - 1; while (l < r) { int mn = min(height[l], height[r]); if (mn == height[l]) { ++l; while (l < r && height[l] < mn) { res += mn - height[l++]; } } else { --r; while (l < r && height[r] < mn) { res += mn - height[r--]; } } } return res; } };
Java 解法二:spa
public class Solution { public int trap(int[] height) { int res = 0, l = 0, r = height.length - 1; while (l < r) { int mn = Math.min(height[l], height[r]); if (height[l] == mn) { ++l; while (l < r && height[l] < mn) { res += mn - height[l++]; } } else { --r; while (l < r && height[r] < mn) { res += mn - height[r--]; } } } return res; } }
咱们能够对上面的解法进行进一步优化,使其更加简介:指针
C++ 解法三:
class Solution { public: int trap(vector<int>& height) { int l = 0, r = height.size() - 1, level = 0, res = 0; while (l < r) { int lower = height[(height[l] < height[r]) ? l++ : r--]; level = max(level, lower); res += level - lower; } return res; } };
Java 解法三:
public class Solution { public int trap(int[] height) { int l = 0, r = height.length - 1, level = 0, res = 0; while (l < r) { int lower = height[(height[l] < height[r]) ? l++ : r--]; level = Math.max(level, lower); res += level - lower; } return res; } }
下面这种解法是用stack来作的,博主一开始都没有注意到这道题的tag还有stack,因此之后在总结的时候仍是要多多留意一下标签啊。其实用stack的方法博主感受更容易理解,咱们的作法是,遍历高度,若是此时栈为空,或者当前高度小于等于栈顶高度,则把当前高度的坐标压入栈,注意咱们不直接把高度压入栈,而是把坐标压入栈,这样方便咱们在后来算水平距离。当咱们遇到比栈顶高度大的时候,就说明有可能会有坑存在,能够装雨水。此时咱们栈里至少有一个高度,若是只有一个的话,那么不能造成坑,咱们直接跳过,若是多余一个的话,那么此时把栈顶元素取出来看成坑,新的栈顶元素就是左边界,当前高度是右边界,只要取两者较小的,减去坑的高度,长度就是右边界坐标减去左边界坐标再减1,两者相乘就是盛水量啦,参见代码以下:
C++ 解法四:
class Solution { public: int trap(vector<int>& height) { stack<int> st; int i = 0, res = 0, n = height.size(); while (i < n) { if (st.empty() || height[i] <= height[st.top()]) { st.push(i++); } else { int t = st.top(); st.pop(); if (st.empty()) continue; res += (min(height[i], height[st.top()]) - height[t]) * (i - st.top() - 1); } } return res; } };
Java 解法四:
class Solution { public int trap(int[] height) { Stack<Integer> s = new Stack<Integer>(); int i = 0, n = height.length, res = 0; while (i < n) { if (s.isEmpty() || height[i] <= height[s.peek()]) { s.push(i++); } else { int t = s.pop(); if (s.isEmpty()) continue; res += (Math.min(height[i], height[s.peek()]) - height[t]) * (i - s.peek() - 1); } } return res; } }
相似题目:
参考资料:
https://discuss.leetcode.com/topic/18731/7-lines-c-c/2
https://discuss.leetcode.com/topic/5125/sharing-my-simple-c-code-o-n-time-o-1-space