Tap here to switch tabs
Problem
Submissions

Problem: Watering Plants II

med
30 min
Try to solve the Watering Plants II problem.

Statement

Alice and Bob want to water nn plants arranged in a row, labeled from index 00 to n−1n - 1 from left to right. The ii-th plant requires plants[i] units of water.

Alice starts at plant 00 moving to the right, while Bob starts at plant n−1n - 1 moving to the left. They begin watering at the same time. Initially, Alice carries a can with a capacity of capacityA units of water, and Bob carries a can with a capacity of capacityB units of water. Both cans are completely full at the start.

They water the plants according to the following process:

  • Alice waters from left to right, and Bob waters from right to left.

  • If a person has enough water to satisfy the current plant, they water it using that amount.

  • If a person does not have enough water to satisfy the current plant, they completely refill their can to its full capacity first and then water the plant.

  • If Alice and Bob meet at the same plant:

    • The person with more remaining water in their can waters the plant.

    • If both have an equal amount of water, Alice waters it.

    • If the designated person does not have enough water, they refill their can before watering.

Return the total number of times Alice and Bob must refill their watering cans to water all the plants.

Constraints:

  • n == plants.length

  • 1≤n≤1051 \leq n \leq 10^5

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

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

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Watering Plants II

med
30 min
Try to solve the Watering Plants II problem.

Statement

Alice and Bob want to water nn plants arranged in a row, labeled from index 00 to n−1n - 1 from left to right. The ii-th plant requires plants[i] units of water.

Alice starts at plant 00 moving to the right, while Bob starts at plant n−1n - 1 moving to the left. They begin watering at the same time. Initially, Alice carries a can with a capacity of capacityA units of water, and Bob carries a can with a capacity of capacityB units of water. Both cans are completely full at the start.

They water the plants according to the following process:

  • Alice waters from left to right, and Bob waters from right to left.

  • If a person has enough water to satisfy the current plant, they water it using that amount.

  • If a person does not have enough water to satisfy the current plant, they completely refill their can to its full capacity first and then water the plant.

  • If Alice and Bob meet at the same plant:

    • The person with more remaining water in their can waters the plant.

    • If both have an equal amount of water, Alice waters it.

    • If the designated person does not have enough water, they refill their can before watering.

Return the total number of times Alice and Bob must refill their watering cans to water all the plants.

Constraints:

  • n == plants.length

  • 1≤n≤1051 \leq n \leq 10^5

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

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