当前位置:网站首页>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

边栏推荐
- [AUTOSAR RTE] - 1-talk about RTE (run time environment)
- 2022T电梯修理考试题及在线模拟考试
- Likeshop takeout order system is open source, 100% open source, no encryption
- Still using xshell? Recommend this more modern terminal connection tool
- .Net CLR GC 动态加载短暂堆阈值的计算及阈值超量的计算
- 多线程学习笔记-1.CAS
- Use notes of Lichuang EDA
- 一文详解MES系统给企业带来的5大好处,附应用场景
- 2022茶艺师(中级)考试题模拟考试题库及答案
- 2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation
猜你喜欢

多商户商城系统功能拆解16讲-平台端会员成长值记录

5款WPS Office最佳海外替代品

Unity 农场 2 —— 种植系统

【考研词汇训练营】Day 14 —— panini,predict,access,apologize,sense,transport,aggregation

Microsoft silently donated $10000 to curl, which was not notified until half a year later

2022g1 industrial boiler stoker certificate question bank and simulation examination

Neural network learning (2) introduction 2

Simulated 100 questions and simulated examination of refrigeration and air conditioning equipment operation examination in 2022

flex布局

2022G1工业锅炉司炉上岗证题库及模拟考试
随机推荐
.Net CLR GC 动态加载短暂堆阈值的计算及阈值超量的计算
Tensor RT's int8 quantization principle
SSM integration - exception handler and project exception handling scheme
MySQL - 多表查询与案例详解
Likeshop takeout order system is open source, 100% open source, no encryption
How to design test cases well
【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引
This section is used to supplement 3
likeshop外卖点餐系统开源啦100%开源无加密
MES系统的选择需重点考虑哪些方面?
Redis学习笔记-2.客户端的使用
MES系统最全介绍来了
Operations research 69 | explanation of classic examples of dynamic planning
MySQL练习题初级45题(统一表)
NFT digital collection system development: fellow uncle first promoted the blessing series digital collection, which will be sold out immediately
深入理解为什么不要使用System.out.println()
JS question brushing plan - linked list
SD NAND与eMMC优劣势对比
The first ABAP ALV reporter construction process
SMMU carding