当前位置:网站首页>LeetCode简单题之装满杯子需要的最短总时长
LeetCode简单题之装满杯子需要的最短总时长
2022-07-26 18:00:00 【·星辰大海】
题目
现有一台饮水机,可以制备冷水、温水和热水。每秒钟,可以装满 2 杯 不同 类型的水或者 1 杯任意类型的水。
给你一个下标从 0 开始、长度为 3 的整数数组 amount ,其中 amount[0]、amount[1] 和 amount[2] 分别表示需要装满冷水、温水和热水的杯子数量。返回装满所有杯子所需的 最少 秒数。
示例 1:
输入:amount = [1,4,2]
输出:4
解释:下面给出一种方案:
第 1 秒:装满一杯冷水和一杯温水。
第 2 秒:装满一杯温水和一杯热水。
第 3 秒:装满一杯温水和一杯热水。
第 4 秒:装满一杯温水。
可以证明最少需要 4 秒才能装满所有杯子。
示例 2:
输入:amount = [5,4,4]
输出:7
解释:下面给出一种方案:
第 1 秒:装满一杯冷水和一杯热水。
第 2 秒:装满一杯冷水和一杯温水。
第 3 秒:装满一杯冷水和一杯温水。
第 4 秒:装满一杯温水和一杯热水。
第 5 秒:装满一杯冷水和一杯热水。
第 6 秒:装满一杯冷水和一杯温水。
第 7 秒:装满一杯热水。
示例 3:
输入:amount = [5,0,0]
输出:5
解释:每秒装满一杯冷水。
提示:
amount.length == 3
0 <= amount[i] <= 100
来源:力扣(LeetCode)
解题思路
对于三种水,我们只需要每次固定将两个剩余最大的水杯减一即可,直到所有的水杯数量都小于1.
class Solution:
def fillCups(self, amount: List[int]) -> int:
count=0
while amount[0]>0 or amount[1]>0 or amount[2]>0 :
amount.sort()
amount[1]-=1
amount[2]-=1
count+=1
return count

边栏推荐
- Exploratory software testing
- 【考研词汇训练营】Day 14 —— panini,predict,access,apologize,sense,transport,aggregation
- SD NAND与eMMC优劣势对比
- Multi merchant mall system function disassembly Lecture 16 - platform side member growth value record
- Flex layout
- [interview question] 1384- share 44 JS problems. Half right is a master
- NFT digital collection system development: sold out when online, and netizens "spike" Digital Collections
- 2022年制冷与空调设备运行操作考试模拟100题及模拟考试
- Paged query design of scenarios
- 2022 mobile crane driver test questions simulation test platform operation
猜你喜欢

MySQL学习笔记-2.如何提高sql语句的查询性能

NFT digital collection system development: fellow uncle first promoted the blessing series digital collection, which will be sold out immediately

The United States, Japan and South Korea jointly developed 6G with the intention of anti surpassing, but China has long been prepared

offer-集合(1)

NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品

2022 chemical automation control instrument test question simulation test platform operation

MPLS experiment

工赋开发者社区 | 定了!就在7月30日!

OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源

14. Gradient detection, random initialization, neural network Summary
随机推荐
JS question brushing plan - linked list
项目中@RequestMapping的作用以及如何使用
NFT数字藏品开发:数字藏品助力企业发展
Flask encapsulates seven cattle cloud
OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源
手写一个Starter
2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation
【在 Kotlin 中添加条件行为】
The class jointly built by famous oarsmen is new, and Professor qiuxipeng of Fudan University broadcast it live on Tuesday!
场景之分页查询设计
NFT digital collection system development: sold out when online, and netizens "spike" Digital Collections
MySQL - multi table query and case explanation
NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品
【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract
Duplicate gallerycms character length limit short domain name bypass
模板进阶(跑路人笔记)
Racher deploys kubernetes cluster
2022g1 industrial boiler stoker certificate question bank and simulation examination
NFT数字藏品系统开发:“中国旅游日”山西首次发布古建筑数字藏品
Comparison of advantages and disadvantages between SD NAND and EMMC