2105. 给植物浇水 II

给植物浇水 II

解法一: 贪心

go
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
func minimumRefill(plants []int, capacityA int, capacityB int) int {
a := capacityA
b := capacityB
ans := 0
left := 0
right := len(plants) - 1
for left <= right {
if left == right {
if a < plants[left] && b < plants[left] {
ans++
}
break
}
if a < plants[left] {
ans++
a = capacityA
}
a -= plants[left]
if b < plants[right] {
ans++
b = capacityB
}
b -= plants[right]
left++
right--
}
return ans
}
作者

wuhunyu

发布于

2024-05-09

更新于

2025-01-15

许可协议