Trapping Rain Water – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Given n
non-negative integers representing an elevation map where the width of each bar is 1
, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) 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.
Example 2:
Input: height = [4,2,0,3,2,5] Output: 9
Constraints:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
C++ Trapping Rain Water LeetCode Solution
class Solution { // 4 ms, faster than 89.31%
public:
int trap(vector<int>& height) {
int n = height.size();
vector<int> leftMax(n), rightMax(n);
for (int i = 1; i < n; ++i)
leftMax[i] = max(height[i-1], leftMax[i-1]);
for (int i = n-2; i >= 0; –i)
rightMax[i] = max(height[i+1], rightMax[i+1]);
int ans = 0;
for (int i = 0; i < n; ++i) {
int waterLevel = min(leftMax[i], rightMax[i]);
if (waterLevel >= height[i]) ans += waterLevel – height[i];
}
return ans;
}
};
Java Trapping Rain Water LeetCode Solution
class Solution { // 1 ms, faster than 85.57%
public int trap(int[] height) {
int n = height.length;
int[] leftMax = new int[n], rightMax = new int[n];
for (int i = 1; i < n; ++i)
leftMax[i] = Math.max(height[i-1], leftMax[i-1]);
for (int i = n-2; i >= 0; –i)
rightMax[i] = Math.max(height[i+1], rightMax[i+1]);
int ans = 0;
for (int i = 0; i < n; ++i) {
int waterLevel = Math.min(leftMax[i], rightMax[i]);
if (waterLevel >= height[i]) ans += waterLevel – height[i];
}
return ans;
}
}
Python 3 Trapping Rain Water LeetCode Solution
class Solution: # 52 ms, faster than 81.89%
def trap(self, height: List[int]) -> int:
n = len(height)
maxLeft, maxRight = [0] * n, [0] * n
for i in range(1, n):
maxLeft[i] = max(height[i-1], maxLeft[i-1])
for i in range(n-2, -1, -1):
maxRight[i] = max(height[i+1], maxRight[i+1])
ans = 0
for i in range(n):
waterLevel = min(maxLeft[i], maxRight[i])
if waterLevel >= height[i]:
ans += waterLevel – height[i]
return ans
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1
Leave a comment below