├── Trapping Rain Water(python).py ├── Trapping Rain Water(c++).cpp ├── Trapping Rain Water(java).java ├── Time Complexity.txt └── Algorithm.txt /Trapping Rain Water(python).py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def trap(self, height: List[int]) -> int: 3 | n = len(height) 4 | l = [0] * n # l[i] := max(height[0..i]) 5 | r = [0] * n # r[i] := max(height[i..n)) 6 | 7 | for i, h in enumerate(height): 8 | l[i] = h if i == 0 else max(h, l[i - 1]) 9 | 10 | for i, h in reversed(list(enumerate(height))): 11 | r[i] = h if i == n - 1 else max(h, r[i + 1]) 12 | 13 | return sum(min(l[i], r[i]) - h 14 | for i, h in enumerate(height)) -------------------------------------------------------------------------------- /Trapping Rain Water(c++).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | const int n = height.size(); 5 | int ans = 0; 6 | vector l(n); // l[i] := max(height[0..i]) 7 | vector r(n); // r[i] := max(height[i..n)) 8 | 9 | for (int i = 0; i < n; ++i) 10 | l[i] = i == 0 ? height[i] : max(height[i], l[i - 1]); 11 | 12 | for (int i = n - 1; i >= 0; --i) 13 | r[i] = i == n - 1 ? height[i] : max(height[i], r[i + 1]); 14 | 15 | for (int i = 0; i < n; ++i) 16 | ans += min(l[i], r[i]) - height[i]; 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Trapping Rain Water(java).java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trap(int[] height) { 3 | final int n = height.length; 4 | int ans = 0; 5 | int[] l = new int[n]; // l[i] := max(height[0..i]) 6 | int[] r = new int[n]; // r[i] := max(height[i..n)) 7 | 8 | for (int i = 0; i < n; ++i) 9 | l[i] = i == 0 ? height[i] : Math.max(height[i], l[i - 1]); 10 | 11 | for (int i = n - 1; i >= 0; --i) 12 | r[i] = i == n - 1 ? height[i] : Math.max(height[i], r[i + 1]); 13 | 14 | for (int i = 0; i < n; ++i) 15 | ans += Math.min(l[i], r[i]) - height[i]; 16 | 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /Time Complexity.txt: -------------------------------------------------------------------------------- 1 | The time complexity of the problem is O(n), where n is the length of the input list `height`. 2 | 3 | The code consists of three main parts: 4 | 5 | 1. Initializing the `l` list: This part iterates over the `height` list once and fills the `l` list with the maximum height encountered from the left. Since this is a single iteration over the list, the time complexity is O(n). 6 | 7 | 2. Initializing the `r` list: This part iterates over the `height` list in reverse order and fills the `r` list with the maximum height encountered from the right. Again, this is a single iteration over the list, so the time complexity is O(n). 8 | 9 | 3. Calculating the trapped water: This part also iterates over the `height` list once, calculating the amount of trapped water for each position. Since this is a single iteration, the time complexity is O(n). 10 | 11 | Overall, the code has a time complexity of O(n) because the three main parts of the code, each with a time complexity of O(n), are executed sequentially. Therefore, the dominant factor in the time complexity is the size of the input list `height`. -------------------------------------------------------------------------------- /Algorithm.txt: -------------------------------------------------------------------------------- 1 | 2 | Here's how the algorithm works: 3 | 4 | 1. Initialize two lists, `l` and `r`, with all elements set to zero. These lists will store the maximum height encountered from the left and the right, respectively, for each position in the input height list. 5 | 6 | 2. Iterate over the `height` list, filling the `l` list with the maximum height encountered from the left. The maximum height for each position `i` is determined by comparing the current height `h` with the maximum height found so far `l[i-1]` (if `i` is not the first element). The maximum height is stored in the `l` list at the corresponding index `i`. 7 | 8 | 3. Iterate over the `height` list in reverse order, filling the `r` list with the maximum height encountered from the right. The maximum height for each position `i` is determined by comparing the current height `h` with the maximum height found so far `r[i+1]` (if `i` is not the last element). The maximum height is stored in the `r` list at the corresponding index `i`. 9 | 10 | 4. Initialize a variable `water` to keep track of the total trapped water, starting with zero. 11 | 12 | 5. Iterate over the `height` list. For each position `i`, calculate the amount of trapped water by taking the minimum of the maximum heights from the left and right (i.e., `min(l[i], r[i])`) and subtracting the current height `h`. Add this calculated amount to the `water` variable. 13 | 14 | 6. Return the final value of `water`, which represents the total amount of trapped rainwater on the given elevation map. 15 | 16 | In summary, the algorithm calculates the maximum heights from the left and right for each position in the input height list and then calculates the trapped water by subtracting the current height from the minimum of the maximum heights from the left and right. Finally, it returns the total trapped water. --------------------------------------------------------------------------------