Watering Plants II
Problem
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
0thplant. Bob waters the plants in order from right to left , starting from the(n - 1)thplant. 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 should water this plant. If they have the same amount of water, then Alice should water this plant.
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 thenumber of times they have to refill to water all the plants.
Examples
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 <= 10^51 <= plants[i] <= 10^6max(plants[i]) <= capacityA, capacityB <= 10^9
Solution
Method 1 – Two Pointers Simulation
Intuition
Use two pointers: one for Alice (left to right), one for Bob (right to left). Simulate the process, refilling as needed. If both meet at the same plant, the one with more water waters it (Alice if equal).
Approach
Initialize two pointers, i (Alice) at 0 and j (Bob) at n-1. Track current water for both. For each step:
- If
i < j, Alice watersi, Bob watersj. - If
i == j, only one waters (the one with more water, Alice if equal). - For each, if not enough water, refill and count. Continue until all plants are watered.
Code
Python
from typing import List
class Solution:
def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:
i, j = 0, len(plants) - 1
a, b = capacityA, capacityB
refills = 0
while i < j:
if a < plants[i]:
refills += 1
a = capacityA
a -= plants[i]
if b < plants[j]:
refills += 1
b = capacityB
b -= plants[j]
i += 1
j -= 1
if i == j:
if max(a, b) < plants[i]:
refills += 1
return refills
Java
class Solution {
public int minimumRefill(int[] plants, int capacityA, int capacityB) {
int i = 0, j = plants.length - 1;
int a = capacityA, b = capacityB, refills = 0;
while (i < j) {
if (a < plants[i]) {
refills++;
a = capacityA;
}
a -= plants[i++];
if (b < plants[j]) {
refills++;
b = capacityB;
}
b -= plants[j--];
}
if (i == j) {
if (Math.max(a, b) < plants[i]) refills++;
}
return refills;
}
}
C++
#include <vector>
using namespace std;
class Solution {
public:
int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
int i = 0, j = plants.size() - 1, a = capacityA, b = capacityB, refills = 0;
while (i < j) {
if (a < plants[i]) {
refills++;
a = capacityA;
}
a -= plants[i++];
if (b < plants[j]) {
refills++;
b = capacityB;
}
b -= plants[j--];
}
if (i == j) {
if (max(a, b) < plants[i]) refills++;
}
return refills;
}
};
Go
func minimumRefill(plants []int, capacityA int, capacityB int) int {
i, j := 0, len(plants)-1
a, b := capacityA, capacityB
refills := 0
for i < j {
if a < plants[i] {
refills++
a = capacityA
}
a -= plants[i]
if b < plants[j] {
refills++
b = capacityB
}
b -= plants[j]
i++
j--
}
if i == j && max(a, b) < plants[i] {
refills++
}
return refills
}
Kotlin
class Solution {
fun minimumRefill(plants: IntArray, capacityA: Int, capacityB: Int): Int {
var i = 0
var j = plants.size - 1
var a = capacityA
var b = capacityB
var refills = 0
while (i < j) {
if (a < plants[i]) {
refills++
a = capacityA
}
a -= plants[i++]
if (b < plants[j]) {
refills++
b = capacityB
}
b -= plants[j--]
}
if (i == j && maxOf(a, b) < plants[i]) refills++
return refills
}
}
Rust
impl Solution {
pub fn minimum_refill(plants: Vec<i32>, capacity_a: i32, capacity_b: i32) -> i32 {
let (mut i, mut j) = (0, plants.len() as i32 - 1);
let (mut a, mut b) = (capacity_a, capacity_b);
let mut refills = 0;
while i < j {
if a < plants[i as usize] {
refills += 1;
a = capacity_a;
}
a -= plants[i as usize];
if b < plants[j as usize] {
refills += 1;
b = capacity_b;
}
b -= plants[j as usize];
i += 1;
j -= 1;
}
if i == j && a.max(b) < plants[i as usize] {
refills += 1;
}
refills
}
}
TypeScript
function minimumRefill(plants: number[], capacityA: number, capacityB: number): number {
let i = 0, j = plants.length - 1;
let a = capacityA, b = capacityB, refills = 0;
while (i < j) {
if (a < plants[i]) {
refills++;
a = capacityA;
}
a -= plants[i++];
if (b < plants[j]) {
refills++;
b = capacityB;
}
b -= plants[j--];
}
if (i === j && Math.max(a, b) < plants[i]) refills++;
return refills;
}
Complexity
- ⏰ Time complexity:
O(n)— Each plant is visited at most once by Alice or Bob. - 🧺 Space complexity:
O(1)— Only a few variables are used.