LeetCode 题解工作台

给植物浇水

你打算用一个水罐给花园里的 n 株植物浇水。植物排成一行,从左到右进行标记,编号从 0 到 n - 1 。其中,第 i 株植物的位置是 x = i 。 x = -1 处有一条河,你可以在那里重新灌满你的水罐。 每一株植物都需要浇特定量的水。你将会按下面描述的方式完成浇水: 按从左到右的顺序给植物浇水…

category

2

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·模拟

bolt

答案摘要

我们可以模拟给植物浇水的过程,用一个变量 表示当前水罐中的水量,初始时 $\textit{water} = \textit{capacity}$。 我们遍历植物,对于每一株植物:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·模拟 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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

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

  • 按从左到右的顺序给植物浇水。
  • 在给当前植物浇完水之后,如果你没有足够的水 完全 浇灌下一株植物,那么你就需要返回河边重新装满水罐。
  • 不能 提前重新灌满水罐。

最初,你在河边(也就是,x = -1),在 x 轴上每移动 一个单位 都需要 一步

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

 

示例 1:

输入:plants = [2,2,3,3], capacity = 5
输出:14
解释:从河边开始,此时水罐是装满的:
- 走到植物 0 (1 步) ,浇水。水罐中还有 3 单位的水。
- 走到植物 1 (1 步) ,浇水。水罐中还有 1 单位的水。
- 由于不能完全浇灌植物 2 ,回到河边取水 (2 步)。
- 走到植物 2 (3 步) ,浇水。水罐中还有 2 单位的水。
- 由于不能完全浇灌植物 3 ,回到河边取水 (3 步)。
- 走到植物 3 (4 步) ,浇水。
需要的步数是 = 1 + 1 + 2 + 3 + 3 + 4 = 14 。

示例 2:

输入:plants = [1,1,1,4,2,3], capacity = 4
输出:30
解释:从河边开始,此时水罐是装满的:
- 走到植物 0,1,2 (3 步) ,浇水。回到河边取水 (3 步)。
- 走到植物 3 (4 步) ,浇水。回到河边取水 (4 步)。
- 走到植物 4 (5 步) ,浇水。回到河边取水 (5 步)。
- 走到植物 5 (6 步) ,浇水。
需要的步数是 = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30 。

示例 3:

输入:plants = [7,7,7,7,7,7,7], capacity = 8
输出:49
解释:每次浇水都需要重新灌满水罐。
需要的步数是 = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49 。

 

提示:

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

解题思路

方法一:模拟

我们可以模拟给植物浇水的过程,用一个变量 water\textit{water} 表示当前水罐中的水量,初始时 water=capacity\textit{water} = \textit{capacity}

我们遍历植物,对于每一株植物:

  • 如果当前水罐中的水量足够浇灌这株植物,我们就向前移动一步,浇灌这株植物,同时更新 water=waterplants[i]\textit{water} = \textit{water} - \textit{plants}[i]
  • 否则我们就需要返回河边重新装满水罐,再次走到当前位置,然后向前移动一步,此时我们需要的步数为 i×2+1i \times 2 + 1,然后我们浇灌这株植物,更新 water=capacityplants[i]\textit{water} = \textit{capacity} - \textit{plants}[i]

最后返回总的步数即可。

时间复杂度 O(n)O(n),其中 nn 为植物的数量。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def wateringPlants(self, plants: List[int], capacity: int) -> int:
        ans, water = 0, capacity
        for i, p in enumerate(plants):
            if water >= p:
                water -= p
                ans += 1
            else:
                water = capacity - p
                ans += i * 2 + 1
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Tests understanding of array manipulation and simulation techniques.

  • question_mark

    Checks the ability to manage state transitions and distance calculations in a real-world scenario.

  • question_mark

    Assesses problem-solving skills related to edge cases, such as large plant water requirements and multiple refills.

warning

常见陷阱

外企场景
  • error

    Failing to correctly simulate refilling the watering can after running out of water for a plant.

  • error

    Overlooking the distance when walking back to the river for refills.

  • error

    Not managing the steps effectively, especially when dealing with large water requirements or plant quantities.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    The array length may vary, increasing the complexity as more plants are added.

  • arrow_right_alt

    The capacity of the watering can may differ, altering the refill frequency.

  • arrow_right_alt

    Consider different walking patterns (e.g., skipping certain plants) to optimize the process.

help

常见问题

外企场景

给植物浇水题解:数组·模拟 | LeetCode #2079 中等