最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

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

LeetCode 2105. Watering Plants II

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

Alice and Bob want to water?n?plants in their garden. 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.

Each plant needs a specific amount of water. Alice and Bob have a watering can each,?initially full. They water the plants in the following way:

  • Alice waters the plants in order from?left to right, starting from the?0th?plant. Bob waters the plants in order from?right to left, starting from the?(n - 1)th?plant. They begin watering the plants?simultaneously.

  • It takes the same amount of time to water each plant regardless of how much water it needs.

  • Alice/Bob?must?water the plant if they have enough in their can to?fully?water it. Otherwise, they?first?refill their can (instantaneously) then water the plant.

  • In case both Alice and Bob reach the same plant, the one with?more?water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice should water this plant.

Given a?0-indexed?integer array?plants?of?n?integers, where?plants[i]?is the amount of water the?ith?plant needs, and two integers?capacityA?and?capacityB?representing the capacities of Alice's and Bob's watering cans respectively, return?the?number of times?they have to refill to water all the plants.

?

Example 1:

Input:?

plants = [2,2,3,3], capacityA = 5, capacityB = 5

Output: 1

Explanation:

- Initially, Alice and Bob have 5 units of water each in their watering cans.

- Alice waters plant 0, Bob waters plant 3.

- Alice and Bob now have 3 units and 2 units of water respectively.?

- Alice has enough water for plant 1, so she waters it. Bob does not have enough water for plant 2, so he refills his can then waters it. So, the total number of times they have to refill to water all the plants is 0 + 0 + 1 + 0 = 1.

Example 2:

Input: plants = [2,2,3,3], capacityA = 3, capacityB = 4

Output: 2

Explanation:

- Initially, Alice and Bob have 3 units and 4 units of water in their watering cans respectively. - Alice waters plant 0, Bob waters plant 3.

- Alice and Bob now have 1 unit of water each, and need to water plants 1 and 2 respectively.

- Since neither of them have enough water for their current plants, they refill their cans and then water the plants.

So, the total number of times they have to refill to water all the plants is 0 + 1 + 1 + 0 = 2.

Example 3:

Input:?

plants = [5], capacityA = 10, capacityB = 8

Output: 0

Explanation:

- There is only one plant.?

- Alice's watering can has 10 units of water, whereas Bob's can has 8 units.?

Since Alice has more water in her can, she waters this plant. So, the total number of times they have to refill is 0.

?

Constraints:

  • n == plants.length

  • 1 <= n <= 105

  • 1 <= plants[i] <= 106

  • max(plants[i]) <= capacityA, capacityB <= 109

主要是判斷2種情況,1種是plant的長(zhǎng)度是奇數(shù),一種是偶數(shù)的情況,

分別去看,然后寫(xiě)兩個(gè)函數(shù),分別是從2端計(jì)算,需要蓄水的次數(shù)跟剩下的水,

最后根據(jù)題意去比對(duì)即可;

下面是代碼:沒(méi)想到速度會(huì)這么快。

Runtime:?4 ms, faster than?99.18%?of?Java?online submissions for?Watering Plants II.

Memory Usage:?58.9 MB, less than?55.74%?of?Java?online submissions for?Watering Plants II.



LeetCode 2105. Watering Plants II的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
宜春市| 鄱阳县| 曲阜市| 肥乡县| 叙永县| 东宁县| 尚志市| 根河市| 珲春市| 林西县| 哈密市| 班戈县| 英吉沙县| 乌恰县| 黔南| 嘉峪关市| 江北区| 余干县| 丰镇市| 克拉玛依市| 苏尼特左旗| 吉木乃县| 文成县| 故城县| 边坝县| 武夷山市| 汽车| 邮箱| 菏泽市| 崇文区| 钟祥市| 墨玉县| 江达县| 罗平县| 辽源市| 古丈县| 鲜城| 三明市| 庆城县| 交城县| 西城区|