返回介绍

solution / 2100-2199 / 2105.Watering Plants II / README_EN

发布于 2024-06-17 01:03:10 字数 6873 浏览 0 评论 0 收藏 0

2105. Watering Plants II

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:
    i, j = 0, len(plants) - 1
    ans = 0
    a, b = capacityA, capacityB
    while i <= j:
      if i == j:
        if max(capacityA, capacityB) < plants[i]:
          ans += 1
        break
      if capacityA < plants[i]:
        capacityA = a - plants[i]
        ans += 1
      else:
        capacityA -= plants[i]
      if capacityB < plants[j]:
        capacityB = b - plants[j]
        ans += 1
      else:
        capacityB -= plants[j]
      i += 1
      j -= 1
    return ans
class Solution {
  public int minimumRefill(int[] plants, int capacityA, int capacityB) {
    int i = 0, j = plants.length - 1;
    int ans = 0, a = capacityA, b = capacityB;
    while (i <= j) {
      if (i == j) {
        if (Math.max(capacityA, capacityB) < plants[i]) {
          ++ans;
        }
        break;
      }
      if (capacityA < plants[i]) {
        capacityA = a - plants[i];
        ++ans;
      } else {
        capacityA -= plants[i];
      }
      if (capacityB < plants[j]) {
        capacityB = b - plants[j];
        ++ans;
      } else {
        capacityB -= plants[j];
      }
      ++i;
      --j;
    }
    return ans;
  }
}
class Solution {
public:
  int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
    int i = 0, j = plants.size() - 1;
    int ans = 0, a = capacityA, b = capacityB;
    while (i <= j) {
      if (i == j) {
        if (max(capacityA, capacityB) < plants[i]) ++ans;
        break;
      }
      if (capacityA < plants[i]) {
        capacityA = a - plants[i];
        ++ans;
      } else
        capacityA -= plants[i];

      if (capacityB < plants[j]) {
        capacityB = b - plants[j];
        ++ans;
      } else
        capacityB -= plants[j];
      ++i;
      --j;
    }
    return ans;
  }
};
func minimumRefill(plants []int, capacityA int, capacityB int) int {
  i, j := 0, len(plants)-1
  ans, a, b := 0, capacityA, capacityB
  for i <= j {
    if i == j {
      if max(capacityA, capacityB) < plants[i] {
        ans++
      }
      break
    }
    if capacityA < plants[i] {
      capacityA = a - plants[i]
      ans++
    } else {
      capacityA -= plants[i]
    }
    if capacityB < plants[j] {
      capacityB = b - plants[j]
      ans++
    } else {
      capacityB -= plants[j]
    }
    i++
    j--
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文