LeetCode 2335. Minimum Amount of Time to Fill Cups
考点 | 难度 |
---|---|
Greedy | Easy |
题目
You have a water dispenser that can dispense cold, warm, and hot water. Every second, you can either fill up 2 cups with different types of water, or 1 cup of any type of water.
You are given a 0-indexed integer array amount of length 3 where amount[0], amount[1], and amount[2] denote the number of cold, warm, and hot water cups you need to fill respectively. Return the minimum number of seconds needed to fill up all the cups.
思路
先sort,最大的两个温度如果都大于1,result加1,最大的两个分别减1,之后重新sort。重复直到只有一个温度大于0,result加剩下的。
答案
class Solution(object):
def fillCups(self, amount):
result = 0
amount.sort()
while amount[1] and amount[2]:
result += 1
amount[1] -= 1
amount[2] -= 1
amount.sort()
result += amount[2]
return result