[LeetCode] 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.
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]相比,如果大於當前值,則將差值存入結果,程式碼如下:
C++ 解法一:
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指標重合,具體參見程式碼如下:
C++ 解法二:
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 解法二:
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; } }
類似題目:
參考資料: