Class Solution

java.lang.Object
g2101_2200.s2105_watering_plants_ii.Solution

public class Solution extends Object
2105 - Watering Plants II.<p>Medium</p> <p>Alice and Bob want to water <code>n</code> plants in their garden. The plants are arranged in a row and are labeled from <code>0</code> to <code>n - 1</code> from left to right where the <code>i<sup>th</sup></code> plant is located at <code>x = i</code>.</p> <p>Each plant needs a specific amount of water. Alice and Bob have a watering can each, <strong>initially full</strong>. They water the plants in the following way:</p> <ul> <li>Alice waters the plants in order from <strong>left to right</strong> , starting from the <code>0<sup>th</sup></code> plant. Bob waters the plants in order from <strong>right to left</strong> , starting from the <code>(n - 1)<sup>th</sup></code> plant. They begin watering the plants <strong>simultaneously</strong>.</li> <li>It takes the same amount of time to water each plant regardless of how much water it needs.</li> <li>Alice/Bob <strong>must</strong> water the plant if they have enough in their can to <strong>fully</strong> water it. Otherwise, they <strong>first</strong> refill their can (instantaneously) then water the plant.</li> <li>In case both Alice and Bob reach the same plant, the one with <strong>more</strong> water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice should water this plant.</li> </ul> <p>Given a <strong>0-indexed</strong> integer array <code>plants</code> of <code>n</code> integers, where <code>plants[i]</code> is the amount of water the <code>i<sup>th</sup></code> plant needs, and two integers <code>capacityA</code> and <code>capacityB</code> representing the capacities of Alice&rsquo;s and Bob&rsquo;s watering cans respectively, return <em>the <strong>number of times</strong> they have to refill to water all the plants</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> plants = [2,2,3,3], capacityA = 5, capacityB = 5</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>Initially, Alice and Bob have 5 units of water each in their watering cans.</p> </li> <li> <p>Alice waters plant 0, Bob waters plant 3.</p> </li> <li> <p>Alice and Bob now have 3 units and 2 units of water respectively.</p> </li> <li> <p>Alice has enough water for plant 1, so she waters it. Bob does not have enough water for plant 2, so he refills his can then waters it.</p> </li> </ul> <p>So, the total number of times they have to refill to water all the plants is 0 + 0 + 1 + 0 = 1.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> plants = [2,2,3,3], capacityA = 3, capacityB = 4</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>Initially, Alice and Bob have 3 units and 4 units of water in their watering cans respectively.</p> </li> <li> <p>Alice waters plant 0, Bob waters plant 3.</p> </li> <li> <p>Alice and Bob now have 1 unit of water each, and need to water plants 1 and 2 respectively.</p> </li> <li> <p>Since neither of them have enough water for their current plants, they refill their cans and then water the plants.</p> </li> </ul> <p>So, the total number of times they have to refill to water all the plants is 0 + 1 + 1 + 0 = 2.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> plants = [5], capacityA = 10, capacityB = 8</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>There is only one plant.</p> </li> <li> <p>Alice&rsquo;s watering can has 10 units of water, whereas Bob&rsquo;s can has 8 units. Since Alice has more water in her can, she waters this plant.</p> </li> </ul> <p>So, the total number of times they have to refill is 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == plants.length</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>1 <= plants[i] <= 10<sup>6</sup></code></li> <li><code>max(plants[i]) <= capacityA, capacityB <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimumRefill

      public int minimumRefill(int[] plants, int capacityA, int capacityB)