LeetCode 2105. Watering Plants II-每日快播
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的长度是奇数,一种是偶数的情况,
分别去看,然后写两个函数,分别是从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-每日快播
哔哩哔哩 2023-05-04
-
“绵阳”“园区提质”“企业满园”行动一周年 涪城交出精彩答卷-世界热议
同心四川 2023-05-04
-
2022年财报披露!15家核心零部件厂商营收、利润一览-环球视讯
中国传动网 2023-05-04
-
焦点观察:茂县群_关于茂县群的简介
互联网 2023-05-04
-
今日聚焦!“5·15全国投资者保护宣传日”优秀投教作品展播征集
证券时报网 2023-05-04
-
中国物流与采购联合会:4月中国仓储指数为53.7% 环比上升3.5个百分点
智通财经 2023-05-04
-
世界新动态:有你的贡献吗? “五一”假期国内旅游出游2.74亿人次
人民网 2023-05-04
-
环球微头条丨2021年是微软第一方游戏阵容 最高光的一年
游戏玩具评论蜀黍 2023-05-04
-
福特 CEO 重申支持 CarPlay,在美国 70% 该品牌车主是苹果客户
搜狐科技 2023-05-04
-
美国亚特兰大枪击案致1死4伤 参议员谈枪支暴力:谁都不安全
海外网 2023-05-04
-
LeetCode 2105. Watering Plants II-每日快播
哔哩哔哩 2023-05-04
-
“绵阳”“园区提质”“企业满园”行动一周年 涪城交出精彩答卷-世界热议
同心四川 2023-05-04
-
2022年财报披露!15家核心零部件厂商营收、利润一览-环球视讯
中国传动网 2023-05-04
-
焦点观察:茂县群_关于茂县群的简介
互联网 2023-05-04
-
今日聚焦!“5·15全国投资者保护宣传日”优秀投教作品展播征集
证券时报网 2023-05-04
-
中国物流与采购联合会:4月中国仓储指数为53.7% 环比上升3.5个百分点
智通财经 2023-05-04
-
世界新动态:有你的贡献吗? “五一”假期国内旅游出游2.74亿人次
人民网 2023-05-04
-
环球微头条丨2021年是微软第一方游戏阵容 最高光的一年
游戏玩具评论蜀黍 2023-05-04
-
福特 CEO 重申支持 CarPlay,在美国 70% 该品牌车主是苹果客户
搜狐科技 2023-05-04
-
美国亚特兰大枪击案致1死4伤 参议员谈枪支暴力:谁都不安全
海外网 2023-05-04
-
平凡岗位上的奋斗故事
人民网 2023-05-04
-
胃炎,不能吃“白菜”吗?医生:养胃护胃,这3菜,建议管住嘴 焦点热闻
蕊说趣聊 2023-05-04
-
天天热资讯!投资海尔之前,你要想好赚哪一种钱
躺平指数 2023-05-04
-
重庆:桥下空间改造为城市增色
经济参考报 2023-05-04
-
鲁西化工双氧水装置爆炸着火 市场影响几何?
界面新闻 2023-05-03
-
世界观天下!新疆乡村增收“领路人”:见证乡村发展“新天地”
中国新闻网 2023-05-03
-
通讯!美国4月ADP就业人数增加29.6万人,为2022年7月以来最大增幅。
中钢网 2023-05-03
-
五一自驾游开什么车最合适?2023款奔腾T99带你玩转春天_每日快讯
搜狐汽车 2023-05-03
-
中银证券给予中青旅增持评级 两镇景区高弹性复苏 后续有望持续受益于客流回升 世界新视野
每日经济新闻 2023-05-03
-
天天即时看!中性粒细胞比值偏低一点_中性粒细胞比值偏低
互联网 2023-05-03
-
截至2022年底全国共有共青团员7358.3万名
中国新闻网 2023-05-03
-
委内瑞拉总统:愿推动委中关系取得更大进展
新华网 2023-05-03
-
做次有钱人电影(做一次有钱人电影)-环球百事通
互联网 2023-05-03
-
每日资讯:涵艺:我认知里S8的RNG没有假赛!S10阿水真的赔了RNG八百万
天下足球 2023-05-03
-
焦点资讯:五一档全国电影票房已突破13亿元
界面新闻 2023-05-03
-
高考倒计时一个月,如何冲刺更有效|前沿资讯
教育信息网 2023-05-03
-
即时:哈尔滨砸承重墙事件:近2亿元的赔偿方案建议,绝不可纳税人兜底
废柴话江湖 2023-05-03
-
星火成炬 | 硬核建设者_每日报道
央视网 2023-05-03
-
五大消息影响节后A股! 当前消息
中金在线财经号 2023-05-03
-
中国共青团团内统计公报发布:全国共有共青团员7358.3万名
央视新闻客户端 2023-05-03