2079. Watering Plants Medium
1/**
2 * [2079] Watering Plants
3 *
4 * 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 i^th plant is located at x = i. There is a river at x = -1 that you can refill your watering can at.
5 * Each plant needs a specific amount of water. You will water the plants in the following way:
6 *
7 * Water the plants in order from left to right.
8 * 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.
9 * You cannot refill the watering can early.
10 *
11 * You are initially at the river (i.e., x = -1). It takes one step to move one unit on the x-axis.
12 * Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the i^th plant needs, and an integer capacity representing the watering can capacity, return the number of steps needed to water all the plants.
13 *
14 * Example 1:
15 *
16 * Input: plants = [2,2,3,3], capacity = 5
17 * Output: 14
18 * Explanation: Start at the river with a full watering can:
19 * - Walk to plant 0 (1 step) and water it. Watering can has 3 units of water.
20 * - Walk to plant 1 (1 step) and water it. Watering can has 1 unit of water.
21 * - Since you cannot completely water plant 2, walk back to the river to refill (2 steps).
22 * - Walk to plant 2 (3 steps) and water it. Watering can has 2 units of water.
23 * - Since you cannot completely water plant 3, walk back to the river to refill (3 steps).
24 * - Walk to plant 3 (4 steps) and water it.
25 * Steps needed = 1 + 1 + 2 + 3 + 3 + 4 = 14.
26 *
27 * Example 2:
28 *
29 * Input: plants = [1,1,1,4,2,3], capacity = 4
30 * Output: 30
31 * Explanation: Start at the river with a full watering can:
32 * - Water plants 0, 1, and 2 (3 steps). Return to river (3 steps).
33 * - Water plant 3 (4 steps). Return to river (4 steps).
34 * - Water plant 4 (5 steps). Return to river (5 steps).
35 * - Water plant 5 (6 steps).
36 * Steps needed = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30.
37 *
38 * Example 3:
39 *
40 * Input: plants = [7,7,7,7,7,7,7], capacity = 8
41 * Output: 49
42 * Explanation: You have to refill before watering each plant.
43 * Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.
44 *
45 *
46 * Constraints:
47 *
48 * n == plants.length
49 * 1 <= n <= 1000
50 * 1 <= plants[i] <= 10^6
51 * max(plants[i]) <= capacity <= 10^9
52 *
53 */
54pub struct Solution {}
55
56// problem: https://leetcode.com/problems/watering-plants/
57// discuss: https://leetcode.com/problems/watering-plants/discuss/?currentPage=1&orderBy=most_votes&query=
58
59// submission codes start here
60
61impl Solution {
62 pub fn watering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
63 0
64 }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn test_2079() {
75 }
76}
77
Back
© 2025 bowen.ge All Rights Reserved.