Skip to content

Practice Watering Plants

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

Medium#2079
LeetCode Problem

Watering Plants

Problem Statement

plants[i] is the water required by plant i. Plants stand at positions 0 through plants.length - 1 and the river is at -1. Begin at the river with a full can of capacity and water plants from left to right. Before moving to the next plant, return to the river to refill only if the remaining water cannot fully water that plant; early refills are forbidden. Return the total unit-distance steps until every plant is watered.

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

Examples

# Example 1

Input
plants = [2,2,3,3], capacity = 5
Output
14

# Example 2

Input
plants = [1,1,1,4,2,3], capacity = 4
Output
30

# Example 3

Input
plants = [7,7,7,7,7,7,7], capacity = 8
Output
49

Constraints

n == plants.length
1 <= n <= 1000
1 <= plants[i] <= 10^6
max(plants[i]) <= capacity <= 10^9

Related LeetCode Problems

Practice more problems similar to Watering Plants.

Review your practice with PhantomCodeAI

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