五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 2079. Watering Plants

2023-05-04 12:38 作者:您是打尖兒還是住店呢  | 我要投稿

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.

?路是要走完的,所以這個(gè)plants的長度是必須要加上去的,剩下的就是要計(jì)算每次折彎的路程即可,只要剩下的水,小于下次要澆水的量,則必須折返,所以加上折返的路程即可。

下面是代碼:

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的評論 (共 條)

分享到微博請遵守國家法律
白河县| 商城县| 荥经县| 揭西县| 麻城市| 定边县| 武乡县| 遂川县| 成都市| 昭苏县| 衡水市| 建昌县| 灌南县| 景德镇市| 澄城县| 故城县| 葫芦岛市| 通山县| 象州县| 永福县| 邛崃市| 阿拉尔市| 中西区| 舒兰市| 屏南县| 衡南县| 普格县| 东莞市| 兰考县| 泗阳县| 左权县| 中西区| 鲜城| 陕西省| 乐山市| 长海县| 周宁县| 涟水县| 濮阳县| 金堂县| 霍林郭勒市|