2105. Watering Plants II

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2105

class Solution:
    def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:
        i, j = 0, len(plants) - 1
        res = 0
        a, b = capacityA, capacityB
        while i <= j:
            if i == j:
                if max(capacityA, capacityB) < plants[i]:
                    res += 1
                break
            if capacityA < plants[i]:
                capacityA = a - plants[i]
                res += 1
            else:
                capacityA -= plants[i]
            if capacityB < plants[j]:
                capacityB = b - plants[j]
                res += 1
            else:
                capacityB -= plants[j]
            i += 1
            j -= 1
        return res