Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i.
Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way:
0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously.Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and two integers capacityA and capacityB representing the capacities of Alice's and Bob's watering cans respectively, return the number of times they have to refill to water all the plants.
Example 1:
Input: plants = [2,2,3,3], capacityA = 5, capacityB = 5 Output: 1 Explanation: - Initially, Alice and Bob have 5 units of water each in their watering cans. - Alice waters plant 0, Bob waters plant 3. - Alice and Bob now have 3 units and 2 units of water respectively. - Alice has enough water for plant 1, so she waters it. Bob does not have enough water for plant 2, so he refills his can then waters it. So, the total number of times they have to refill to water all the plants is 0 + 0 + 1 + 0 = 1.
Example 2:
Input: plants = [2,2,3,3], capacityA = 3, capacityB = 4 Output: 2 Explanation: - Initially, Alice and Bob have 3 units and 4 units of water in their watering cans respectively. - Alice waters plant 0, Bob waters plant 3. - Alice and Bob now have 1 unit of water each, and need to water plants 1 and 2 respectively. - Since neither of them have enough water for their current plants, they refill their cans and then water the plants. So, the total number of times they have to refill to water all the plants is 0 + 1 + 1 + 0 = 2.
Example 3:
Input: plants = [5], capacityA = 10, capacityB = 8 Output: 0 Explanation: - There is only one plant. - Alice's watering can has 10 units of water, whereas Bob's can has 8 units. Since Alice has more water in her can, she waters this plant. So, the total number of times they have to refill is 0.
Constraints:
n == plants.length1 <= n <= 1051 <= plants[i] <= 106max(plants[i]) <= capacityA, capacityB <= 109Problem summary: Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way: Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously. It takes the same amount of time to water each plant regardless of how much water it needs. Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise, they first refill their can (instantaneously) then water the plant. In case both Alice and Bob reach the same plant, the one with more water currently in his/her watering can
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[2,2,3,3] 5 5
[2,2,3,3] 3 4
[5] 10 8
watering-plants)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2105: Watering Plants II
class Solution {
public int minimumRefill(int[] plants, int capacityA, int capacityB) {
int a = capacityA, b = capacityB;
int ans = 0;
int i = 0, j = plants.length - 1;
for (; i < j; ++i, --j) {
if (a < plants[i]) {
++ans;
a = capacityA;
}
a -= plants[i];
if (b < plants[j]) {
++ans;
b = capacityB;
}
b -= plants[j];
}
ans += i == j && Math.max(a, b) < plants[i] ? 1 : 0;
return ans;
}
}
// Accepted solution for LeetCode #2105: Watering Plants II
func minimumRefill(plants []int, capacityA int, capacityB int) (ans int) {
a, b := capacityA, capacityB
i, j := 0, len(plants)-1
for ; i < j; i, j = i+1, j-1 {
if a < plants[i] {
ans++
a = capacityA
}
a -= plants[i]
if b < plants[j] {
ans++
b = capacityB
}
b -= plants[j]
}
if i == j && max(a, b) < plants[i] {
ans++
}
return
}
# Accepted solution for LeetCode #2105: Watering Plants II
class Solution:
def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:
a, b = capacityA, capacityB
ans = 0
i, j = 0, len(plants) - 1
while i < j:
if a < plants[i]:
ans += 1
a = capacityA
a -= plants[i]
if b < plants[j]:
ans += 1
b = capacityB
b -= plants[j]
i, j = i + 1, j - 1
ans += i == j and max(a, b) < plants[i]
return ans
// Accepted solution for LeetCode #2105: Watering Plants II
impl Solution {
pub fn minimum_refill(plants: Vec<i32>, capacity_a: i32, capacity_b: i32) -> i32 {
let mut a = capacity_a;
let mut b = capacity_b;
let mut ans = 0;
let mut i = 0;
let mut j = plants.len() - 1;
while i < j {
if a < plants[i] {
ans += 1;
a = capacity_a;
}
a -= plants[i];
if b < plants[j] {
ans += 1;
b = capacity_b;
}
b -= plants[j];
i += 1;
j -= 1;
}
if i == j && a.max(b) < plants[i] {
ans += 1;
}
ans
}
}
// Accepted solution for LeetCode #2105: Watering Plants II
function minimumRefill(plants: number[], capacityA: number, capacityB: number): number {
let [a, b] = [capacityA, capacityB];
let ans = 0;
let [i, j] = [0, plants.length - 1];
for (; i < j; ++i, --j) {
if (a < plants[i]) {
++ans;
a = capacityA;
}
a -= plants[i];
if (b < plants[j]) {
++ans;
b = capacityB;
}
b -= plants[j];
}
ans += i === j && Math.max(a, b) < plants[i] ? 1 : 0;
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.