Skip to content

Commit 39620df

Browse files
authored
Update 755-pour-water.js
1 parent 3cfbd15 commit 39620df

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

755-pour-water.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,39 @@ const pourWater = function (heights, V, K) {
1818
heights[bottom]++
1919
return pourWater(heights, V - 1, K)
2020
}
21-
2221
for (let i = K + 1; i < heights.length; i++) {
2322
if (heights[i] > heights[bottom]) break
2423
if (heights[i] < heights[bottom]) bottom = i
2524
}
2625
heights[bottom]++
2726
return pourWater(heights, V - 1, K)
2827
}
28+
29+
// another
30+
31+
/**
32+
* @param {number[]} heights
33+
* @param {number} V
34+
* @param {number} K
35+
* @return {number[]}
36+
*/
37+
const pourWater = function (heights, V, K) {
38+
let cur = K
39+
for (let i = 0; i < V; i++) {
40+
// Move left
41+
while (cur > 0 && heights[cur - 1] <= heights[cur]) {
42+
cur--
43+
}
44+
// Move right
45+
while (cur < heights.length - 1 && heights[cur + 1] <= heights[cur]) {
46+
cur++
47+
}
48+
// Move left to K
49+
while(cur > K && heights[cur - 1] === heights[cur]) {
50+
cur--
51+
}
52+
heights[cur]++
53+
}
54+
return heights
55+
}
56+

0 commit comments

Comments
 (0)