-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainWater.js
More file actions
71 lines (64 loc) · 1.91 KB
/
Copy pathTrappingRainWater.js
File metadata and controls
71 lines (64 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @fileoverview
* Trapping Rain Water
* (LeetCode Hard: 42. Trapping Rain Water)
*
* Target: 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.
*/
/**
* Strategy: Two Pointers
* 1. Maintain two pointers, left and right, at the ends of the array.
* 2. Maintain leftMax and rightMax to track the highest bars on each side.
* 3. Move the pointer with the smaller height, as the water level
* is limited by the lower boundary.
* 4. At each step, if height[left] < leftMax, water trapped = leftMax - height[left].
*
* @param {number[]} height
* @return {number}
*/
function trap(height) {
if (!height || height.length < 3) return 0;
let left = 0;
let right = height.length - 1;
let leftMax = 0;
let rightMax = 0;
let totalWater = 0;
while (left < right) {
if (height[left] < height[right]) {
// Process left side
if (height[left] >= leftMax) {
leftMax = height[left];
} else {
totalWater += leftMax - height[left];
}
left++;
} else {
// Process right side
if (height[right] >= rightMax) {
rightMax = height[right];
} else {
totalWater += rightMax - height[right];
}
right--;
}
}
return totalWater;
}
/**
* 📈 Complexity Analysis:
* -----------------------
* Time Complexity: O(N) - We visit each element once.
* Space Complexity: O(1) - We only use a few constant variables.
*
* 💡 Other Approaches:
* - Brute Force: O(N^2)
* - Dynamic Programming: O(N) Time, O(N) Space (storing leftMax/rightMax arrays).
* - Monotonic Stack: O(N) Time, O(N) Space.
*/
// ------------------------------------
// 🧪 Test Cases
// ------------------------------------
console.log("[0,1,0,2,1,0,1,3,2,1,2,1] :", trap([0,1,0,2,1,0,1,3,2,1,2,1])); // 6
console.log("[4,2,0,3,2,5] :", trap([4,2,0,3,2,5])); // 9