2335. Minimum Amount of Time to Fill Cups — Daily Problem
Problem
2335. Minimum Amount of Time to Fill Cups
Approach
-
This problem is actually quite simple, though I didn't use a greedy approach initially. Looking at the second test case, I realized the key insight: minimize the chance of any count reaching 0. So keep sorting and always operate on the two largest numbers. This also handles edge cases naturally. But because of the sorting, I'm unsure if it scales well for very large inputs.
Looking at ylb's solution — same idea but with much cleaner edge case handling, two fewer conditionals than mine. The mathematical approach below may be the intended solution for harder cases.
-
Mathematical approach? Sort the three drink counts from small to large as
x, y, z. The goal is to pair different drinks as often as possible.- If
x + y <= z, the answer isz. - Otherwise, let
t = (x + y - z). Iftis even, the answer is ; otherwise .
- If
Code
class Solution:
def fillCups(self, amount: List[int]) -> int:
amount.sort()
count = 0
# Try to avoid reaching 0
while amount[-1] > 0:
if amount[-1] > 0 and amount[1] > 0:
amount[-1] -= 1
amount[1] -= 1
count += 1
if amount[-1] > 0 and amount[1] == 0:
return count + amount[-1]
amount.sort()
return countimport "sort"
func fillCups(amount []int) int {
ans := 0
for amount[0] + amount[1] + amount[2] > 0 {
sort.Ints(amount)
ans ++
amount[2] --
if amount[1] > 0{
amount[1] --
}
}
return ans
}贡献者
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0
2309. The best English letters with both appropriates and lowercases One question daily
LeetCode 2309. 兼具大小写的最好英文字母 题解 — 使用哈希表与位运算两种解法,哈希表法从 Z 开始逆序查找字母是否同时出现大小写,位运算法用两个整数掩码分别记录小写和大写字母出现情况,再通过与运算找出最高位对应字母。适合正在刷 LeetCode 每日一题、想掌握哈希表与位运算技巧的求职者和算法学习者。
2341. Maximum Number of Pairs in Array — Daily Problem
LeetCode 2341. Maximum Number of Pairs in Array — Hash table or sorting approach to count pairs and leftovers, for developers practicing array frequency problems.