|
| 1 | +// # Non optimal solution... we dont need aux arrays to keep track of leftMax and rightMax |
| 2 | +// # class Solution { |
| 3 | +// # public: |
| 4 | +// # int trap(vector<int>& height) { |
| 5 | + |
| 6 | +// # if(height.size() == 0) return 0; |
| 7 | +// # int n = height.size(); |
| 8 | +// # int leftMax[n], rightMax[n]; |
| 9 | + |
| 10 | +// # leftMax[0] = height[0]; |
| 11 | +// # rightMax[n-1] = height[n-1]; |
| 12 | + |
| 13 | +// # for(int i=1; i<n; i++) { |
| 14 | +// # leftMax[i] = max(height[i], leftMax[i-1]); |
| 15 | +// # } |
| 16 | + |
| 17 | +// # for(int i=n-2; i>=0; i--) { |
| 18 | +// # rightMax[i] = max(height[i], rightMax[i+1]); |
| 19 | +// # } |
| 20 | + |
| 21 | +// # int totalWater = 0; |
| 22 | +// # for(int i=0; i<n; i++) { |
| 23 | +// # totalWater += min(leftMax[i], rightMax[i]) - height[i]; |
| 24 | +// # } |
| 25 | + |
| 26 | +// # return totalWater; |
| 27 | +// # } |
| 28 | +// # }; |
| 29 | + |
| 30 | + |
| 31 | +// class Solution: |
| 32 | +// def trap(self, height: List[int]) -> int: |
| 33 | + |
| 34 | +// left, right = 0, len(height) - 1 |
| 35 | +// maxLeft, maxRight = 0, 0 |
| 36 | +// waterTrap = 0 |
| 37 | + |
| 38 | +// while left <= right: |
| 39 | +// if height[left] <= height[right]: |
| 40 | +// if height[left] > maxLeft: |
| 41 | +// maxLeft = height[left] |
| 42 | +// else: |
| 43 | +// waterTrap += maxLeft - height[left] |
| 44 | +// left += 1 |
| 45 | +// else: |
| 46 | +// if height[right] > maxRight: |
| 47 | +// maxRight = height[right] |
| 48 | +// else: |
| 49 | +// waterTrap += maxRight - height[right] |
| 50 | +// right -= 1 |
| 51 | + |
| 52 | +// return waterTrap |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +class Solution { |
| 60 | +public: |
| 61 | + int trap(vector<int>& height) { |
| 62 | + |
| 63 | + //Method 1: Use two arrays to keep track of maxToLeft and maxToRight |
| 64 | + /* |
| 65 | + int n = height.size(); |
| 66 | + if(n == 0) return 0; |
| 67 | + |
| 68 | + vector<int> maxToRight(n), maxToLeft(n); |
| 69 | + int waterTrap = 0; |
| 70 | + |
| 71 | + maxToLeft[0] = height[0]; |
| 72 | + for(int i = 1; i < n; i++) { |
| 73 | + maxToLeft[i] = max(height[i], maxToLeft[i - 1]); |
| 74 | + } |
| 75 | + |
| 76 | + maxToRight[n - 1] = height[n - 1]; |
| 77 | + for(int i = n - 2; i >= 0; i--) { |
| 78 | + maxToRight[i] = max(height[i], maxToRight[i + 1]); |
| 79 | + } |
| 80 | + |
| 81 | + for(int i = 0 ; i < n; i++) { |
| 82 | + waterTrap += min(maxToLeft[i], maxToRight[i]) - height[i]; |
| 83 | + } |
| 84 | + |
| 85 | + return waterTrap; |
| 86 | + */ |
| 87 | + |
| 88 | + //Method 2: Use two pointers left and right (instead of ararys) to keep track of maxToLeft and maxToRight |
| 89 | + /* |
| 90 | + int left = 0, right = height.size() - 1; |
| 91 | + int maxLeft = 0, maxRight = 0, waterTrap = 0; |
| 92 | + |
| 93 | + while(left <= right) { |
| 94 | + if(height[left] <= height[right]) { |
| 95 | + if(height[left] > maxLeft) |
| 96 | + maxLeft = height[left]; |
| 97 | + else |
| 98 | + waterTrap += maxLeft - height[left]; |
| 99 | + left++; |
| 100 | + } |
| 101 | + else { |
| 102 | + if(height[right] > maxRight) |
| 103 | + maxRight = height[right]; |
| 104 | + else |
| 105 | + waterTrap += maxRight - height[right]; |
| 106 | + right--; |
| 107 | + } |
| 108 | + } |
| 109 | + return waterTrap; |
| 110 | + */ |
| 111 | + |
| 112 | + //Method 3: Least intuitive - using stack |
| 113 | + |
| 114 | + int length = height.size(); |
| 115 | + int verticalHeight, leftBound, horizontalWidth, waterTrap = 0, currentIndex, prevTop; |
| 116 | + stack<int> indexes; |
| 117 | + |
| 118 | + int i = 0; |
| 119 | + while(i < length) { |
| 120 | + currentIndex = i; |
| 121 | + |
| 122 | + //found a increasing sequence - calculate maxArea |
| 123 | + while(!indexes.empty() and height[currentIndex] > height[indexes.top()]) { |
| 124 | + |
| 125 | + prevTop = height[indexes.top()]; |
| 126 | + indexes.pop(); |
| 127 | + |
| 128 | + if(indexes.empty()) |
| 129 | + break; |
| 130 | + |
| 131 | + horizontalWidth = currentIndex - indexes.top() - 1; |
| 132 | + verticalHeight = min(height[currentIndex], height[indexes.top()]) - height[prevTop]; |
| 133 | + |
| 134 | + waterTrap += verticalHeight * horizontalWidth; |
| 135 | + } |
| 136 | + |
| 137 | + //when its strictly monotonically decreasing sequence of heights |
| 138 | + indexes.push(currentIndex); |
| 139 | + i++; |
| 140 | + } |
| 141 | + return waterTrap; |
| 142 | + } |
| 143 | +}; |
0 commit comments