Skip to content

Commit 176c7d3

Browse files
authored
Create watering-plants-ii.py
1 parent 3e22a0d commit 176c7d3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Python/watering-plants-ii.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def minimumRefill(self, plants, capacityA, capacityB):
6+
"""
7+
:type plants: List[int]
8+
:type capacityA: int
9+
:type capacityB: int
10+
:rtype: int
11+
"""
12+
result = 0
13+
left, right = 0, len(plants)-1
14+
canA, canB = capacityA, capacityB
15+
while left < right:
16+
if canA < plants[left]:
17+
result += 1
18+
canA = capacityA
19+
canA -= plants[left]
20+
if canB < plants[right]:
21+
result += 1
22+
canB = capacityB
23+
canB -= plants[right]
24+
left, right = left+1, right-1
25+
if left == right:
26+
if max(canA, canB) < plants[left]:
27+
result += 1
28+
return result

0 commit comments

Comments
 (0)