[LeetCode] 2105. Watering Plants II

CNoodle發表於2024-05-10

Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i.

Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way:
Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously.
It takes the same amount of time to water each plant regardless of how much water it needs.
Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise, they first refill their can (instantaneously) then water the plant.
In case both Alice and Bob reach the same plant, the one with more 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.
Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and two integers capacityA and capacityB representing the capacities of Alice's and Bob's watering cans respectively, return the number of times they have to refill to water all the plants.

Example 1:
Input: plants = [2,2,3,3], capacityA = 5, capacityB = 5
Output: 1
Explanation:

  • Initially, Alice and Bob have 5 units of water each in their watering cans.
  • Alice waters plant 0, Bob waters plant 3.
  • Alice and Bob now have 3 units and 2 units of water respectively.
  • 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.
    So, the total number of times they have to refill to water all the plants is 0 + 0 + 1 + 0 = 1.

Example 2:
Input: plants = [2,2,3,3], capacityA = 3, capacityB = 4
Output: 2
Explanation:

  • Initially, Alice and Bob have 3 units and 4 units of water in their watering cans respectively.
  • Alice waters plant 0, Bob waters plant 3.
  • Alice and Bob now have 1 unit of water each, and need to water plants 1 and 2 respectively.
  • Since neither of them have enough water for their current plants, they refill their cans and then water the plants.
    So, the total number of times they have to refill to water all the plants is 0 + 1 + 1 + 0 = 2.

Example 3:
Input: plants = [5], capacityA = 10, capacityB = 8
Output: 0
Explanation:

  • There is only one plant.
  • Alice's watering can has 10 units of water, whereas Bob's can has 8 units. Since Alice has more water in her can, she waters this plant.
    So, the total number of times they have to refill is 0.

Constraints:
n == plants.length
1 <= n <= 105
1 <= plants[i] <= 106
max(plants[i]) <= capacityA, capacityB <= 109

給植物澆水 II。

Alice 和 Bob 打算給花園裡的 n 株植物澆水。植物排成一行,從左到右進行標記,編號從 0 到 n - 1 。其中,第 i 株植物的位置是 x = i 。

每一株植物都需要澆特定量的水。Alice 和 Bob 每人有一個水罐,最初是滿的 。他們按下面描述的方式完成澆水:

Alice 按 從左到右 的順序給植物澆水,從植物 0 開始。Bob 按 從右到左 的順序給植物澆水,從植物 n - 1 開始。他們 同時 給植物澆水。
無論需要多少水,為每株植物澆水所需的時間都是相同的。
如果 Alice/Bob 水罐中的水足以 完全 灌溉植物,他們 必須 給植物澆水。否則,他們 首先(立即)重新裝滿罐子,然後給植物澆水。
如果 Alice 和 Bob 到達同一株植物,那麼當前水罐中水 更多 的人會給這株植物澆水。如果他倆水量相同,那麼 Alice 會給這株植物澆水。
給你一個下標從 0 開始的整數陣列 plants ,陣列由 n 個整陣列成。其中,plants[i] 為第 i 株植物需要的水量。另有兩個整數 capacityA 和 capacityB 分別表示 Alice 和 Bob 水罐的容量。返回兩人澆灌所有植物過程中重新灌滿水罐的 次數 。

思路

我是先做了版本一2079題才來做這道題的。原本以為從一個人澆水變成兩個人澆水,還是讓你算需要的步數,結果這道題其實讓你算的是兩個人補水的次數。

既然 Alice 和 Bob 分別從花園的兩端往中間走,那麼這道題的總體思路還是雙指標往中間逼近。無論是 Alice 還是 Bob,只要他們當前水桶裡的水不能滿足其當前所在位置的澆花需求,這個人就需要補水(補水次數 + 1)。

這道題最後的 corner case 是當 Alice 和 Bob 相遇的時候,如果這個位置還未澆水,需要再補水(補水次數 + 1)。不過如果他倆水量相同,那麼 Alice 優先給這株植物澆水這個條件似乎沒有用上。

複雜度

時間O(n)
空間O(1)

程式碼

Java實現

class Solution {
    public int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int n = plants.length;
        int alice = 0;
        int bob = n - 1;
        int bucketA = capacityA;
        int bucketB = capacityB;
        int res = 0;
        while (alice < bob) {
            if (bucketA < plants[alice]) {
                res++;
                bucketA = capacityA;
            }
            bucketA -= plants[alice++];

            if (bucketB < plants[bob]) {
                res++;
                bucketB = capacityB;
            }
            bucketB -= plants[bob--];
        }
        if (alice == bob && Math.max(bucketA, bucketB) < plants[alice]) {
            res++;
        }
        return res;
    }
}

相關文章