Leetcode 42 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.
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!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
這個題的意思是計算可以盛水的多少,可以使用雙指標,單次迴圈法,壓棧方法。
1)
class Solution {
public int trap(int[] height) {
if(height == null && height.length == 0){
return 0;
}
int size = 0;
int max = 0;
int[] column = new int[height.length];
for(int i = 0 ; i < height.length; ++i){
column[i] = max;
max = Math.max(max,height[i]);
}
max = 0;
for(int i = height.length - 1 ; i >= 0; --i){
column[i] = Math.min(max,column[i]);
max = Math.max(max,height[i]);
size += column[i] > height[i] ? Math.abs(column[i] - height[i]) : 0;
}
return size;
}
}
2)雙指標
class Solution {
public int trap(int[] A) {
int n = A.length;
int left = 0, right = n - 1;
int maxLeft = 0, maxRight = 0;
int ans = 0;
while (left < right) {
if (A[left] < A[right]) {
if (A[left] >= maxLeft) {
maxLeft = A[left];
} else {
ans += maxLeft - A[left];
}
left ++;
} else {
if (A[right] >= maxRight) {
maxRight = A[right];
} else {
ans += maxRight - A[right];
}
right --;
}
}
return ans;
}
}
3)
class Solution {
//11.20
public int trap(int[] height) {
Stack<Integer> st = new Stack();
int lastCounted = -1, lastCountedHeight, w,h;
int area = 0;
for (int i = 0; i< height.length; i++) {
int curHeight = height[i];
if (curHeight == 0)
continue;
while (!st.isEmpty()) {
int lastBlock = st.peek();
// System.out.format("===%d %d %d %d %d %d\n", i, area, lastBlock, height[lastBlock],curHeight,st.size());
if (height[lastBlock] > curHeight) {
w = i - lastBlock - 1;
h = curHeight;//as curHeihgt is smaller
lastCountedHeight = 0;
if (lastCounted > lastBlock)
lastCountedHeight = height[lastCounted];
lastCounted = i;
area = area+ (w * h) - (w * lastCountedHeight);
// System.out.format("%d %d %d %d %d %d\n", i, area, w, h, lastCountedHeight,st.size());
st.push(i);
break;
}
st.pop();
w = i - lastBlock - 1;
h = height[lastBlock];
lastCountedHeight = 0;
if (lastCounted > lastBlock)
lastCountedHeight = height[lastCounted];
lastCounted = lastBlock;
area = area + (w * h) - (w * lastCountedHeight);
//System.out.format("--%d %d %d %d %d %d\n", i, area, w, h, lastCountedHeight, st.size());
}
//System.out.println(i);
if (st.isEmpty()) {
st.push(i);
}
}
return area;
}
}
注意細節,兩端的節點。
相關文章
- LeetCode 42. Trapping Rain WaterLeetCodeAPPAI
- Leetcode 之 PHP 解析 (42. Trapping Rain Water)LeetCodePHPAPPAI
- Trapping Rain WaterAPPAI
- Leetcode Trapping Raining waterLeetCodeAPPAI
- Trapping-rain-waterAPPAI
- LeetCode-Water and Jug ProblemLeetCode
- leetcode Container With Most WaterLeetCodeAI
- [LeetCode] 417. Pacific Atlantic Water FlowLeetCode
- Leetcode 11 Container With Most WaterLeetCodeAI
- Leetcode-Container With Most WaterLeetCodeAI
- Container With Most Water leetcode javaAILeetCodeJava
- leetcode_11. Container With Most WaterLeetCodeAI
- LeetCode 11. Container With Most WaterLeetCodeAI
- LeetCode - Medium - 11. Container With Most WaterLeetCodeAI
- 每日leetcode——42. 接雨水LeetCode
- [LeetCode] Container With Most Water 裝最多水的容器LeetCodeAI
- LeetCode Container With Most Water(011)解法總結LeetCodeAI
- LeetCode 第 42 場雙週賽 ( rank 514 / 1578 )LeetCode
- C. Mixing Water
- leetcode 417. Pacific Atlantic Water Flow 太平洋大西洋水流問題LeetCode
- 11. Container With Most WaterAI
- 水紋效果(Water)源程式. (轉)
- LeetCode 1326. Minimum Number of Taps to Open to Water a Garden 動態規劃 離散化 貪心LeetCode動態規劃
- vue42Vue
- 42.zip
- constraint=constraints?AI
- PostgreSQL DBA(42) - localeSQL
- Tkinter (42) 控制變數變數
- 清醒時刻記 42
- CSP模擬賽 #42
- Kiss the rainAI
- trainAI
- TrainerAI
- Codeforces Round 689(Div.2) E題 Water Level
- Oracle段高水位(HWM, high water mark)問題Oracle
- 沙因的睡蓮模型(Water lily model) (轉載)模型
- Android ConstraintLayout 2.0:ConstraintLayoutStatesAndroidAI
- 每週分享第 42 期