File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments