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

边栏推荐
- JS刷题计划——链表
- 2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation
- SSM整合-异常处理器和项目异常处理方案
- The class jointly built by famous oarsmen is new, and Professor qiuxipeng of Fudan University broadcast it live on Tuesday!
- 项目中@RequestMapping的作用以及如何使用
- MySQL - function and constraint commands
- Microsoft silently donated $10000 to curl, which was not notified until half a year later
- 【在 Kotlin 中添加条件行为】
- [interview question] 1384- share 44 JS problems. Half right is a master
- Test cases of common functions
猜你喜欢

Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here

Comparison of advantages and disadvantages between SD NAND and EMMC

Unity 农场 2 —— 种植系统

2022年制冷与空调设备运行操作考试模拟100题及模拟考试

NFT digital collection system development: sold out when online, and netizens "spike" Digital Collections

FTP协议

Daorayaki | product principles of non-financial decentralized application

JS刷题计划——数组

VTK (the Visualization Toolkit) loads STL models

分布式事务-seata
随机推荐
MySQL - 函数及约束命令
SMMU carding
MySQL - multi table query and case explanation
PMP candidates must read, and the epidemic prevention requirements for the exam on July 30 are here
MySQL exercises elementary 45 questions (Unified table)
MES系统的选择需重点考虑哪些方面?
【在 Kotlin 中添加条件行为】
Use notes of Lichuang EDA
2022年云商店联合营销市场发展基金(MDF)介绍
2022上海市安全员C证操作证考试题库模拟考试平台操作
NFT digital collection development: digital collections help enterprise development
Complete MySQL database commands
2022年流动式起重机司机考试试题模拟考试平台操作
.net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
2022 welder (elementary) operation certificate examination question bank and simulation examination
神经网络学习(2)前言介绍二
【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract
The United States, Japan and South Korea jointly developed 6G with the intention of anti surpassing, but China has long been prepared
Arrangement of information security emergency plan
Pyqt5 rapid development and practice 3.5 menu bar and toolbar