当前位置:首页> 滚动 >

LeetCode 2105. Watering Plants II 环球动态

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

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


(资料图片仅供参考)

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 0thplant. Bob waters the plants in order from right to left, starting from the (n - 1)thplant. 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 plantsof nintegers, where plants[i]is the amount of water the ithplant needs, and two integers capacityAand capacityBrepresenting 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的长度是奇数,一种是偶数的情况,

分别去看,然后写两个函数,分别是从2端计算,需要蓄水的次数跟剩下的水,

最后根据题意去比对即可;

下面是代码:没想到速度会这么快。

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 环球动态

AliceandBobwanttowaternplantsintheirgarden Theplantsarearrangedinarowandarelabeledfrom0ton-1fromleft

2023-05-04

福建2023年成考报名要花多少钱,怎么缴纳费用_聚焦

福建2023年成考报名要花多少钱,怎么缴纳费用今年福建成人高考的报名费用在高起专考生应缴纳报名考务费每生

2023-05-04

中信证券点评4月中采PMI数据:制造业景气水平有所回落,非制造业延续复苏

中信证券认为,4月,制造业和非制造业复苏呈现分化态势,非制造业整体表现好于制造业。本月制造业采购经理

2023-05-04

世界看热讯:全线开江!嫩江松花江黑龙江防凌工作顺利结束

人民网北京5月4日电(欧阳易佳)据水利部消息,近日,黑龙江干流上游三合江段开江。至此,嫩江、松花江、黑

2023-05-04

【全球热闻】2023年“五一”假期国内旅游出游2.74亿人次,国内旅游收入1480.56亿元

央视网消息:2023年“五一”假期,文化和旅游行业复苏势头强劲,全国假日市场平稳有序。经文化和旅游部数据

2023-05-04

当前消息!一季度我国原煤生产稳定增长,进口大幅增加

【电缆网讯】2023年1-3月,我国原煤生产稳定增长,进口大幅增加。据国家统计局统计,2023年1-3月,规模以上

2023-05-04

厂通路潮白河大桥施工,“五一”不休加紧建设|全球新消息

北京日报客户端|记者韩梅通讯员马彦铭“五一”假日期间,连接京冀的厂通路潮白河大桥项目施工现场一片繁忙

2023-05-04

全国共有共青团员7358.3万名-世界信息

人民日报北京5月3日电(记者杨昊)共青团中央3日发布的中国共青团团内统计公报显示,截至2022年12月底,全

2023-05-04

丽水中心城区3.9亿元挂牌1宗商住地 预计5月26日出让-当前信息

观点网讯。日前,浙江丽水中心城区挂牌1宗商住地,地块起价3 9亿元,起始楼面价11183元 ㎡。地块位于中心城

2023-05-04

当前动态:美联储上调联邦基金利率目标区间25个基点

美联储上调联邦基金利率目标区间25个基点

2023-05-04