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