Watering Plants II
specialist · 930 · lc medium +32 · verified · 48.6% accepted · 316 likes · top 35%
Description
Alice and Bob are watering n plants arranged in a row, labeled 0 to n - 1. Each plant requires a specific amount of water. Alice starts from the left (plant 0) and Bob starts from the right (plant n - 1); both move toward the center simultaneously.
Watering follows these rules:
- Alice waters plants left to right and Bob waters plants right to left. Both start with full cans.
- Every plant takes the same amount of time to water regardless of how much water it needs.
- A person must water the plant if they have enough water; otherwise, they refill their can instantly before watering.
- If both Alice and Bob arrive at the same plant, the one with more water in their can waters it. If they have equal water, Alice waters it.
Given a 0-indexed integer array plants of n integers, where plants[i] is the water needed by plant i, and two integers capacityA and capacityB representing the can capacities, return the total number of times they must refill their cans.
Example 1:
Example 2:
Example 3:
Code