Skip to content

Commit 2fba060

Browse files
aQuaaQua
authored andcommitted
756 accepted. 4ms
1 parent 43f5064 commit 2fba060

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

Algorithms/0756.pour-water/pour-water.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@ package Problem0756
33
func pourWater(heights []int, V int, K int) []int {
44
for V > 0 {
55
V--
6-
drop(heights, K, heights[K]+1, true, true)
6+
if !isDroppedLeft(heights, K) && !isDroppedRight(heights, K) {
7+
heights[K]++
8+
}
79
}
810
return heights
911
}
1012

11-
func drop(h []int, i, j int, l, r bool) bool {
12-
if l && i > 0 && h[i-1] <= h[i] && drop(h, i-1, h[i], l, false) {
13-
return true
13+
func isDroppedLeft(heights []int, center int) bool {
14+
idx := center
15+
i := center
16+
for 0 <= i-1 && heights[i-1] <= heights[i] {
17+
if heights[i-1] < heights[i] {
18+
idx = i - 1
19+
}
20+
i--
1421
}
15-
if r && i < len(h)-1 && h[i+1] <= h[i] && drop(h, i+1, h[i], false, r) {
22+
if idx < center {
23+
heights[idx]++
1624
return true
1725
}
26+
return false
27+
}
1828

19-
if h[i] == j {
20-
return false
29+
func isDroppedRight(heights []int, center int) bool {
30+
idx := center
31+
i := center
32+
for i+1 < len(heights) && heights[i] >= heights[i+1] {
33+
if heights[i] > heights[i+1] {
34+
idx = i + 1
35+
}
36+
i++
2137
}
22-
23-
h[i]++
24-
return true
38+
if center < idx {
39+
heights[idx]++
40+
return true
41+
}
42+
return false
2543
}

0 commit comments

Comments
 (0)