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
- Leetcode 11 Container With Most WaterLeetCodeAI
- LeetCode 11. Container With Most WaterLeetCodeAI
- leetcode_11. Container With Most WaterLeetCodeAI
- [LeetCode] 417. Pacific Atlantic Water FlowLeetCode
- LeetCode - Medium - 11. Container With Most WaterLeetCodeAI
- LeetCode Container With Most Water(011)解法總結LeetCodeAI
- 每日leetcode——42. 接雨水LeetCode
- Save Water
- LeetCode 第 42 場雙週賽 ( rank 514 / 1578 )LeetCode
- leetcode 417. Pacific Atlantic Water Flow 太平洋大西洋水流問題LeetCode
- C. Mixing Water
- LeetCode 1326. Minimum Number of Taps to Open to Water a Garden 動態規劃 離散化 貪心LeetCode動態規劃
- 【題解】「CSP模擬賽」雨天 rainAI
- Like Sunday, Like Rain - JavaScript運算子優先順序AIJavaScript
- vue42Vue
- RAIN:虛擬銷售的技能和挑戰報告AI
- PostgreSQL DBA(42) - localeSQL
- Day42--ArrayList
- Codeforces Round 689(Div.2) E題 Water Level
- 清醒時刻記 42
- Tkinter (42) 控制變數變數
- CSP模擬賽 #42
- laravel報錯的問題 42S01 420004 42S22]等...Laravel
- 團隊即時戰略遊戲《A YEAR OF RAIN》封測版今日開啟遊戲AI
- sqli-Labs————less-42SQL
- 42_Docker容器編排Docker
- 專案微管理42 - 人本
- 每週分享第 42 期
- 七年級英語作文05____My favourite Water Festival
- Day42--試卷錯題
- Day42--四捨五入
- Water 2.5 釋出,一站式服務治理平臺
- Water 2.5.8 釋出,一站式服務治理平臺
- Water 2.6.3 釋出,一站式服務治理平臺