Tap here to switch tabs
Problem
Submissions

Problem: Watering Plants

med
30 min
Try to solve the Watering Plants problem.

Statement

You want to water nn plants in a row, indexed from 00 to n−1n - 1 from left to right. The plant at index ii is situated at coordinate ii and needs plants[i] units of water. You are given an integer array plants and an integer capacity representing the maximum capacity of your watering can.

You start at the river located at coordinate −1-1 with a full watering can. You must water the plants according to the following rules:

Water the plants in order from left to right, starting at plant 00 and ending at plant n−1n - 1.
Moving between adjacent coordinates takes 11 step.
If your watering can contains enough water to fully water plant ii, move to coordinate ii and water it.
If your watering can does not have enough water to fully water plant ii, return to the river at coordinate −1-1 to fully refill the can, then walk to coordinate ii to water it.
You cannot refill your watering can unless you cannot completely water the current plant.

Return the total number of steps required to water all the plants.

Constraints:

  • n == plants.length

  • 1≤1 \leq n ≤1000\leq 1000

  • 1≤1 \leq plants[i] ≤106\leq 10^6

  • max⁡(\max(plants[i])≤) \leq capacity ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Watering Plants

med
30 min
Try to solve the Watering Plants problem.

Statement

You want to water nn plants in a row, indexed from 00 to n−1n - 1 from left to right. The plant at index ii is situated at coordinate ii and needs plants[i] units of water. You are given an integer array plants and an integer capacity representing the maximum capacity of your watering can.

You start at the river located at coordinate −1-1 with a full watering can. You must water the plants according to the following rules:

Water the plants in order from left to right, starting at plant 00 and ending at plant n−1n - 1.
Moving between adjacent coordinates takes 11 step.
If your watering can contains enough water to fully water plant ii, move to coordinate ii and water it.
If your watering can does not have enough water to fully water plant ii, return to the river at coordinate −1-1 to fully refill the can, then walk to coordinate ii to water it.
You cannot refill your watering can unless you cannot completely water the current plant.

Return the total number of steps required to water all the plants.

Constraints:

  • n == plants.length

  • 1≤1 \leq n ≤1000\leq 1000

  • 1≤1 \leq plants[i] ≤106\leq 10^6

  • max⁡(\max(plants[i])≤) \leq capacity ≤109\leq 10^9