↓Skip to main content
  1. LeetCode/

LeetCode 2105: Watering Plants II

·2 mins· ·
LeetCode Medium Two-Pointers Simulation Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-24

  • Submissions: Accepted (first try)

Approach
#

Both start together and every plant takes the same time, so at step t Alice is at t and Bob at n - 1 - t: converging pointers.

Each step:

  • With enough water, water the plant; otherwise refill first (count +1), then water.
  • left == right (the middle plant of an odd-length row): whoever has more water waters it, Alice on a tie. If that person has enough there is no refill; otherwise one refill.

My Solution
#

class Solution:
    def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:
        a, b = capacityA, capacityB
        left, right = 0, len(plants) - 1
        refills = 0
        while left < right:
            if a < plants[left]:
                a = capacityA
                refills += 1
            a -= plants[left]
            if b < plants[right]:
                b = capacityB
                refills += 1
            b -= plants[right]
            left += 1
            right -= 1
        if left == right and max(a, b) < plants[left]:
            refills += 1                       # middle plant: refill only if the fuller can is short
        return refills

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Refilling with a = capacityA and forgetting to subtract this plant’s water.
  • Letting both water the middle plant, or always giving it to Alice.

Interview Explanation
#

They move at the same speed, so Alice from the left and Bob from the right are converging pointers. At each step, if the water is not enough, refill first and count it. With an odd length they meet in the middle, where the one with more water waters it, and a refill is needed only if even that is not enough. Time O(n).

Related

LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 3775: Reverse Words With Same Vowel Count
·1 min
LeetCode Medium String Simulation Two-Pointers
LeetCode 3775 note: count the first word’s vowels, then reverse every later word with the same count
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself