[LeetCode] 2079. Watering Plants

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

给植物浇水。

你打算用一个水罐给花园里的 n 株植物浇水。植物排成一行,从左到右进行标记,编号从 0 到 n - 1 。其中,第 i 株植物的位置是 x = i 。x = -1 处有一条河,你可以在那里重新灌满你的水罐。

每一株植物都需要浇特定量的水。你将会按下面描述的方式完成浇水:

按从左到右的顺序给植物浇水。
在给当前植物浇完水之后,如果你没有足够的水 完全 浇灌下一株植物,那么你就需要返回河边重新装满水罐。
你 不能 提前重新灌满水罐。
最初,你在河边(也就是,x = -1),在 x 轴上每移动 一个单位 都需要 一步 。

给你一个下标从 0 开始的整数数组 plants ,数组由 n 个整数组成。其中,plants[i] 为第 i 株植物需要的水量。另有一个整数 capacity 表示水罐的容量,返回浇灌所有植物需要的 步数 。

思路

按照题意模拟,遍历 plants 数组,具体参见代码注释。

复杂度

时间O(n)
空间O(1)

代码

Java实现

class Solution {
    public int wateringPlants(int[] plants, int capacity) {
        int res = 0;
        int bucket = capacity;
        int i = 0;
        while (i < plants.length) {
            int plant = plants[i];
			// 如果当前水不够,返回河边打水,再走回来
            if (bucket < plant) {
                res += i * 2;
                bucket = capacity;
            }
			// 往前走一步,浇水,再看下一个位置
            res++;
            bucket -= plant;
            i++;
        }
        return res;
    }
}
posted @ 2024-05-08 14:46  CNoodle  阅读(3)  评论(0编辑  收藏  举报