@@ -62,16 +62,58 @@ be stored in every element of array. For example, consider the array
62
62
` [3, 0, 0, 2, 0, 4] ` , We can trap "3* 2 units" of water between 3 an 2, "1 unit"
63
63
on top of bar 2 and "3 units" between 2 and 4. See below diagram also.
64
64
65
- A ** simple solution** is to traverse every array element and find the highest
66
- bars on left and right sides. Take the smaller of two heights. The difference
67
- between smaller height and height of current element is the amount of water
68
- that can be stored in this array element. Time complexity of this solution
69
- is ` O(n2) ` .
65
+ ### Approach 1: Brute force
70
66
71
- An ** efficient solution** is to pre-compute highest bar on left and right of
72
- every bar in ` O(n) ` time. Then use these pre-computed values to find the
73
- amount of water in every array element.
67
+ ** Intuition**
68
+
69
+ For each element in the array, we find the maximum level of water it can trap
70
+ after the rain, which is equal to the minimum of maximum height of bars on both
71
+ the sides minus its own height.
72
+
73
+ ** Steps**
74
+
75
+ - Initialize ` answer = 0 `
76
+ - Iterate the array from left to right:
77
+ - Initialize ` max_left = 0 and ` max_right = 0`
78
+ - Iterate from the current element to the beginning of array updating: ` max_left = max(max_left, height[j]) `
79
+ - Iterate from the current element to the end of array updating: ` max_right = max(max_right, height[j]) `
80
+ - Add ` min(max_left, max_right) − height[i] ` to ` answer `
81
+
82
+ ** Complexity Analysis**
83
+
84
+ Time complexity: ` O(n^2) ` . For each element of array, we iterate the left and right parts.
85
+
86
+ Auxiliary space complexity: ` O(1) ` extra space.
87
+
88
+ ### Approach 2: Dynamic Programming
89
+
90
+ ** Intuition**
91
+
92
+ In brute force, we iterate over the left and right parts again and again just to
93
+ find the highest bar size up to that index. But, this could be stored. Voila,
94
+ dynamic programming.
95
+
96
+ So we may pre-compute highest bar on left and right of every bar in ` O(n) ` time.
97
+ Then use these pre-computed values to find the amount of water in every array element.
98
+
99
+ The concept is illustrated as shown:
100
+
101
+ ![ DP Trapping Rain Water] ( https://leetcode.com/problems/trapping-rain-water/Figures/42/trapping_rain_water.png )
102
+
103
+ ** Steps**
104
+
105
+ - Find maximum height of bar from the left end up to an index ` i ` in the array ` left_max ` .
106
+ - Find maximum height of bar from the right end up to an index ` i ` in the array ` right_max ` .
107
+ - Iterate over the ` height ` array and update ` answer ` :
108
+ - Add ` min(max_left[i], max_right[i]) − height[i] ` to ` answer ` .
109
+
110
+ ** Complexity Analysis**
111
+
112
+ Time complexity: ` O(n) ` .
113
+
114
+ Auxiliary space complexity: ` O(n) ` extra space.
74
115
75
116
## References
76
117
77
118
- [ GeeksForGeeks] ( https://www.geeksforgeeks.org/trapping-rain-water/ )
119
+ - [ LeetCode] ( https://leetcode.com/problems/trapping-rain-water/solution/ )
0 commit comments