標籤:style class blog code http java
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!
方法一:
時間複雜度 O(n),空間複雜度 O(n)
對於每個柱子,找到其左右兩邊最高的柱子,該柱子能容納的面積就是 min(leftMostHeight,rightMostHeight) - height。所以,
1. 從左往右掃描一遍,對於每個柱子,求取左邊最大值;
2. 從右往左掃描一遍,對於每個柱子,求最大右值;
3. 再掃描一遍,把每個柱子的面積並累加。
code:
1 class Solution { 2 public: 3 int trap(int A[], int n) 4 { 5 vector<int> left; 6 vector<int> right; 7 left.resize(n); 8 right.resize(n); 9 10 //printArray(A, n);11 12 left[0] == 0;13 right[n-1] == 0;14 15 // get the left‘s max 16 for(int i = 1; i< n;i++)17 {18 left[i] = max(left[i-1], A[i-1]);19 }20 //printVector(left);21 22 // get the right‘s max 23 for(int i = n-2; i>=0;i--)24 {25 right[i] = max(right[i+1], A[i+1]);26 }27 //printVector(right);28 29 // clac the trap water30 int sum =0;31 int height = 0;32 for(int i = 0; i< n;i++)33 {34 height = min(left[i], right[i]);35 if(height > A[i])36 sum += height - A[i];37 }38 return sum;39 }40 };
方法二:
時間複雜度 O(n),空間複雜度 O(1)
1. 掃描一遍,找到最高的柱子,這個柱子將數組分為兩半;
2. 處理左邊一半;
3. 處理右邊一半。
1 class Solution { 2 public: 3 int trap(int A[], int n) { 4 int max = 0; 5 for(int i = 0; i < n; i++) 6 if (A[i] > A[max]) max = i; 7 int water = 0; 8 int left_max_height = 0; 9 // calc the left_max_height, at the same time update water10 for (int i = 0; i < max; i++)11 if (A[i] > left_max_height) 12 left_max_height = A[i];13 else 14 water += left_max_height - A[i];15 int right_max_height = 0;16 // calc the right_max_height, at the same time update water17 for (int i = n - 1; i > max; i--)18 if (A[i] > right_max_height) 19 right_max_height = A[i];20 else 21 water += right_max_height - A[i];22 return water;23 } 24 };
方法3:
// LeetCode, Trapping Rain Water
// 用一個棧輔助,小於棧頂的元素壓入,大於等於棧頂就把棧裡所有小於或
// 等於當前值的元素全部出棧處理掉,計算面積,最後把當前元素入棧
// 時間複雜度 O(n),空間複雜度 O(n)
可以用std::pair 代替 struct Node結構
1 struct Node 2 { 3 int val; 4 int index; 5 Node(){} 6 Node(int v, int idx):val(v), index(idx){} 7 }; 8 9 class Solution {10 public:11 int trap(int A[], int n) {12 stack<Node> s;13 int sum = 0;14 for(int i = 0; i < n; i++)15 {16 int height = 0;17 // 將棧裡比當前元素矮或等高的元素全部處理掉18 while(!s.empty())19 {20 Node node = s.top();21 // 碰到了比當前元素高的,先計算面積,如{4,3,2},再跳出while迴圈, 將當前元素壓棧22 if (A[i] < node.val ) // A[] = {4, 2,3}, calc sum23 {24 int width = i - node.index - 1;25 sum += A[i] * width - height * width;26 break;27 }28 else29 {30 // node.val, height, a[i] 三者夾成的凹陷31 int width = i - node.index - 1;32 sum += node.val * width - height * width;33 height = node.val;// update height34 // 彈出棧頂,因為該元素處理完了,不再需要了35 s.pop();36 }37 }38 // 所有元素都要入棧39 s.push(Node(A[i], i));40 }41 42 return sum;43 }44 };