当前位置:主页 > 聚焦 >

每日视讯:LeetCode 2079. Watering Plants

时间: 2023-05-04 12:55:05 来源: 哔哩哔哩

You want to water nplants in your garden with a watering can. The plants are arranged in a row and are labeled from 0to n - 1from left to right where the ithplant is located at x = i. There is a river at x = -1that 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 plantsof nintegers, where plants[i]is the amount of water the ithplant needs, and an integer capacityrepresenting 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.

路是要走完的,所以这个plants的长度是必须要加上去的,剩下的就是要计算每次折弯的路程即可,只要剩下的水,小于下次要浇水的量,则必须折返,所以加上折返的路程即可。

下面是代码:

Constraints:

n == plants.length

1 <= n <= 1000

1 <= plants[i] <= 106

max(plants[i]) <= capacity <= 109

Runtime: 0 ms, faster than 100.00% of Java online submissions for Watering Plants.

Memory Usage: 42.5 MB, less than 24.56% of Java online submissions for Watering Plants.

关键词:

相关文章

每日视讯:LeetCode 2079. Watering Plants

Youwanttowaternplantsinyourgardenwithawateringcan Theplantsarearrangedinarowandarelabeledfrom0ton-1f

来源:哔哩哔哩2023-05-04

广州白云区小学2023招生政策汇总|世界视点

白云区小学招生政策汇总(2023年)2023广州白云区小学入学报名指南2023广州白云区公办小学招生地段+计划(

来源:本地宝2023-05-04

匠心独运,大器天成 |明基激光电视智造美好生活 当前速读

坚守初心,只做精品随着人们居家生活空间越来越大,布局越来越丰富,生活方式也随之改变,投影便成为近些年

来源:砍柴网2023-05-04

快消息!田诗医生简介资料出炉了~坐诊威海正源整形医院

一、田诗医生简介坐诊:威海正源整形医院田诗医生是一名认证专家拥有丰富的社会职责,担任着整形美容协会会

来源:一美网2023-05-04

天天热资讯!玩游戏、知识竞猜 主题团日活动丰富职工文化生活

4月27日,中建二局一公司陕西分公司团委开展“永远跟党走奋进新征程”主题团日活动。陕西分公司总部及西安片

来源:中国日报网 -->2023-05-04