Skip to content

Practice Watering Plants II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2105
LeetCode Problem

Watering Plants II

Problem Statement

plants[i] is the water needed by plant i. Alice and Bob begin with full cans of capacityA and capacityB and water simultaneously from the left and right ends, respectively. Each plant takes one equal-time step. Before a plant, refill only if the current water is insufficient. If both reach the same plant, whoever currently has more water waters it, with Alice winning ties. Return the total refill count.

Source: Watering Plants II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
plants = [2,2,3,3], capacityA = 5, capacityB = 5
Output
1

# Example 2

Input
plants = [2,2,3,3], capacityA = 3, capacityB = 4
Output
2

# Example 3

Input
plants = [5], capacityA = 10, capacityB = 8
Output
0

Constraints

n == plants.length
1 <= n <= 10^5
1 <= plants[i] <= 10^6
max(plants[i]) <= capacityA, capacityB <= 10^9

Related LeetCode Problems

Practice more problems similar to Watering Plants II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.