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.length
1 <= n <= 105
1 <= plants[i] <= 106
max(plants[i]) <= capacityA, capacityB <= 109
This problem involves simulating the process of Alice and Bob watering plants from opposite ends. The key is efficiently tracking their water levels and refills. A two-pointer approach significantly optimizes the solution.
Approach:
Initialization:
a
: Alice's water level (starts at capacityA
).b
: Bob's water level (starts at capacityB
).ans
: Number of refills (initialized to 0).i
: Pointer for Alice (starts at the beginning of plants
).j
: Pointer for Bob (starts at the end of plants
).Iteration: The while i < j
loop simulates the simultaneous watering.
a < plants[i]
, Alice needs a refill (ans++
), and a
is reset to capacityA
.a -= plants[i]
).i
increments.b < plants[j]
, Bob needs a refill (ans++
), and b
is reset to capacityB
.b -= plants[j]
).j
decrements.Middle Plant: If i
and j
become equal, there's a middle plant.
max(a, b) < plants[i]
, whoever has less water needs a refill before watering the middle plant (ans++
).Return: The function returns ans
, the total number of refills.
Time Complexity: O(n), where n is the number of plants. We iterate through the plants
array once.
Space Complexity: O(1), as we use a constant number of variables regardless of the input size.
Code Examples (Python, Java, C++, Go, TypeScript, Rust):
The provided code snippets in various languages implement this two-pointer approach. They all follow the same logic described above, with minor syntactic differences based on the language. Each code snippet accurately reflects the steps outlined in the approach section. The comments within the code further clarify the logic at each step.