Skip to content

Commit 3cfbd15

Browse files
authored
Create 755-pour-water.js
1 parent 1e5baae commit 3cfbd15

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

755-pour-water.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} heights
3+
* @param {number} V
4+
* @param {number} K
5+
* @return {number[]}
6+
*/
7+
const pourWater = function (heights, V, K) {
8+
if (!V) return heights
9+
let bottom = K
10+
//iterate through from k to find thee lowest bottom
11+
for (let i = K; i >= 0; i--) {
12+
if (heights[i] > heights[bottom]) break
13+
if (heights[i] < heights[bottom]) bottom = i
14+
}
15+
//if bottom is not k increase height of bottom
16+
//and run again but decrease water droplet V by one
17+
if (bottom !== K) {
18+
heights[bottom]++
19+
return pourWater(heights, V - 1, K)
20+
}
21+
22+
for (let i = K + 1; i < heights.length; i++) {
23+
if (heights[i] > heights[bottom]) break
24+
if (heights[i] < heights[bottom]) bottom = i
25+
}
26+
heights[bottom]++
27+
return pourWater(heights, V - 1, K)
28+
}

0 commit comments

Comments
 (0)