This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

5201. Water the plants

You plan to use a pitcher to water n plants in your garden. Plants are arranged in a row, labeled from left to right and numbered from 0 to N-1. Where the position of the ith plant is x = I. There’s a river at x = -1 where you can refill your pitcher.

Each plant needs a specific amount of water. You will complete the watering as described below:

  • Water the plants from left to right.
  • After watering the current plant, if you don’t have enough water to fully water the next plant, then you need to go back to the river and refill the pitcher.
  • You can’t refill the pitcher ahead of time.

Initially, you are at the river (that is, x = -1), and you need to take a step for every unit you move along the X-axis.

I’m going to give you an array of integers, plants, which has n integers. Plants [I] is the amount of water needed by the ith plant. An integer capacity represents the capacity of the tank and returns the number of steps required to water all the plants.

 

Example 1: input: plants = [2,2,3,3], capacity = 5 output: 14 explanation: start from the river where the pitcher is full: - go to plant 0 (step 1) and water. There are 3 units of water left in the pitcher. - Walk to plant 1 (step 1) and water. I have 1 unit of water left in my pitcher. - Unable to water plants completely 2, return to river for water (2 steps). - Walk to the plant 2 (3 steps) and water it. I have 2 units of water left in the tank. - Unable to water the plants completely 3, return to the river for water (3 steps). - Walk to the plant 3 (4 steps) and water. The number of steps required is = 1 + 1 + 2 + 3 + 3 + 4 = 14. Example 2: input: plants = [1,1,1,4,2,3], capacity = 4 output: 30 explanation: start from the river where the pitcher is full: - go to plant 0,1,2 (3 steps) and water. Return to the river for water (3 steps). - Walk to the plant 3 (4 steps) and water. Return to the river for water (4 steps). - Walk to the plant 4 (5 steps) and water it. Return to the river for water (5 steps). - Walk to plant 5 (6 steps) and water. The number of steps required is = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30. Example 3: Input: plants = [7,7,7,7,7,7], Capacity = 8 The required number of steps is = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.Copy the code

Tip:

  • n == plants.length
  • 1 <= n <= 1000
  • 1 <= plants[i] <=
    1 0 6 10^6
  • max(plants[i]) <= capacity <=
    1 0 9 10^9

Their thinking

When we walk to a plant, we judge whether the water in the water pot is enough. If we find insufficient water, we need to return to the river and refill the water pot. The number of steps once and for all is 2*(I +1)-1, and I is the subscript of the current water-deficient plant.

code

class Solution {
public:
    int wateringPlants(vector<int>& plants, int capacity) {
        int cur=capacity,res=0;
        for (int i = 0; i < plants.size(a); ++i) {if (cur<plants[i]){
                res+=(2*(i+1)- 1);
                cur=capacity-plants[i];
            }else{ cur-=plants[i]; res++; }}returnres; }};Copy the code