-
Notifications
You must be signed in to change notification settings - Fork 0
/
trapping_rain_water.js
44 lines (42 loc) · 1.55 KB
/
trapping_rain_water.js
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
// https://leetcode.com/problems/trapping-rain-water/
// Runtime: 56 ms, faster than 91.49% of JavaScript online submissions for Trapping Rain Water.
// Memory Usage: 34.9 MB, less than 100.00% of JavaScript online submissions for Trapping Rain Water.
function solution(A) {
if (A.length < 2) return 0;
const sumIntermediateCols = (arr, h, g, start, end) => {
let sum = 0;
for (let i = start + 1; i < end; i++) sum += arr[i];
return h * g - sum;
}
const findNextCol = (arr, colIdx, colVal) => {
let max = 0;
for (let i = colIdx + 1; i < arr.length; i++) {
if (arr[i] >= colVal) {
return i; //return the next column at least as high as A[i]
} else {
max = Math.max(max, arr[i]); //track the highest remaining column
}
}
//return index of max if found, otherwise colIdx as last resort
return Math.max(arr.indexOf(max, colIdx), colIdx);
}
const calculator = (arr) => {
let raindrops = 0;
let gap = 0;
let height = 0;
let nextCol = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
nextCol = findNextCol(arr, i, arr[i]);
if (nextCol !== i) {
gap = nextCol - i - 1;
height = Math.min(arr[i], arr[nextCol]);
raindrops += sumIntermediateCols(arr, height, gap, i, nextCol);
i = nextCol - 1;
}
}
}
return raindrops;
}
return calculator(A);
}