返回介绍

solution / 2000-2099 / 2079.Watering Plants / README_EN

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

2079. Watering Plants

中文文档

Description

You want to water n plants in your garden with a watering can. 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. There is a river at x = -1 that you can refill your watering can at.

Each plant needs a specific amount of water. You will water the plants in the following way:

  • Water the plants in order from left to right.
  • After watering the current plant, if you do not have enough water to completely water the next plant, return to the river to fully refill the watering can.
  • You cannot refill the watering can early.

You are initially at the river (i.e., x = -1). It takes one step to move one unit on the x-axis.

Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and an integer capacity representing the watering can capacity, return _the number of steps needed to water all the plants_.

 

Example 1:

Input: plants = [2,2,3,3], capacity = 5
Output: 14
Explanation: Start at the river with a full watering can:
- Walk to plant 0 (1 step) and water it. Watering can has 3 units of water.
- Walk to plant 1 (1 step) and water it. Watering can has 1 unit of water.
- Since you cannot completely water plant 2, walk back to the river to refill (2 steps).
- Walk to plant 2 (3 steps) and water it. Watering can has 2 units of water.
- Since you cannot completely water plant 3, walk back to the river to refill (3 steps).
- Walk to plant 3 (4 steps) and water it.
Steps needed = 1 + 1 + 2 + 3 + 3 + 4 = 14.

Example 2:

Input: plants = [1,1,1,4,2,3], capacity = 4
Output: 30
Explanation: Start at the river with a full watering can:
- Water plants 0, 1, and 2 (3 steps). Return to river (3 steps).
- Water plant 3 (4 steps). Return to river (4 steps).
- Water plant 4 (5 steps). Return to river (5 steps).
- Water plant 5 (6 steps).
Steps needed = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30.

Example 3:

Input: plants = [7,7,7,7,7,7,7], capacity = 8
Output: 49
Explanation: You have to refill before watering each plant.
Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.

 

Constraints:

  • n == plants.length
  • 1 <= n <= 1000
  • 1 <= plants[i] <= 106
  • max(plants[i]) <= capacity <= 109

Solutions

Solution 1

class Solution:
  def wateringPlants(self, plants: List[int], capacity: int) -> int:
    ans, cap = 0, capacity
    for i, x in enumerate(plants):
      if cap >= x:
        cap -= x
        ans += 1
      else:
        cap = capacity - x
        ans += i * 2 + 1
    return ans
class Solution {
  public int wateringPlants(int[] plants, int capacity) {
    int ans = 0, cap = capacity;
    for (int i = 0; i < plants.length; ++i) {
      if (cap >= plants[i]) {
        cap -= plants[i];
        ++ans;
      } else {
        ans += (i * 2 + 1);
        cap = capacity - plants[i];
      }
    }
    return ans;
  }
}
class Solution {
public:
  int wateringPlants(vector<int>& plants, int capacity) {
    int ans = 0, cap = capacity;
    for (int i = 0; i < plants.size(); ++i) {
      if (cap >= plants[i]) {
        cap -= plants[i];
        ++ans;
      } else {
        cap = capacity - plants[i];
        ans += i * 2 + 1;
      }
    }
    return ans;
  }
};
func wateringPlants(plants []int, capacity int) int {
  ans, cap := 0, capacity
  for i, x := range plants {
    if cap >= x {
      cap -= x
      ans++
    } else {
      cap = capacity - x
      ans += i*2 + 1
    }
  }
  return ans
}
function wateringPlants(plants: number[], capacity: number): number {
  const n = plants.length;
  let ans = 0;
  let water = capacity;
  for (let i = 0; i < n; i++) {
    if (water < plants[i]) {
      ans += i * 2 + 1;
      water = capacity - plants[i];
    } else {
      ans++;
      water -= plants[i];
    }
  }
  return ans;
}
impl Solution {
  pub fn watering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
    let n = plants.len();
    let mut ans = 0;
    let mut water = capacity;
    for i in 0..n {
      if water < plants[i] {
        ans += 2 * i + 1;
        water = capacity - plants[i];
      } else {
        ans += 1;
        water -= plants[i];
      }
    }
    ans as i32
  }
}
int wateringPlants(int* plants, int plantsSize, int capacity) {
  int ans = 0;
  int water = capacity;
  for (int i = 0; i < plantsSize; i++) {
    if (water < plants[i]) {
      ans += i * 2 + 1;
      water = capacity - plants[i];
    } else {
      ans++;
      water -= plants[i];
    }
  }
  return ans;
}

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

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

发布评论

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