EasyNo. 2335
Minimum Amount of Time to Fill Cups — LeetCode 2335 Python Solution
- Pattern
- Heap / Priority Queue
- Topics
- Greedy, Array, Sorting, Heap (Priority Queue)
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- amount = [1,4,2]
- Output
- 4
- Explanation
- One way to fill up the cups is:
Python solution
class Solution:
def fillCups(self, amount: List[int]) -> int:
ans = 0
while sum(amount):
amount.sort()
ans += 1
amount[2] -= 1
amount[1] = max(0, amount[1] - 1)
return ansComplexity
- Time
- O(n log n)
- Space · auxiliary
- O(1) to O(n)
Pattern: Heap / Priority Queue
Keep only the best k elements, or always pull the smallest, in log time. LeetCode 2335. Minimum Amount of Time to Fill Cups is filed here because LeetCode tags it Heap (Priority Queue), which is the vocabulary this hub collects.
The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0502 | IPO | Hard |
| 0630 | Course Schedule III | Hard |
| 0857 | Minimum Cost to Hire K Workers | Hard |
| 1353 | Maximum Number of Events That Can Be Attended | Medium |
| 1383 | Maximum Performance of a Team | Hard |
| 1686 | Stone Game VI | Medium |
Frequently asked questions
- How hard is LeetCode 2335. Minimum Amount of Time to Fill Cups?
LeetCode 2335. Minimum Amount of Time to Fill Cups is rated Easy on LeetCode.
- What topics does LeetCode 2335. Minimum Amount of Time to Fill Cups cover?
LeetCode 2335. Minimum Amount of Time to Fill Cups is tagged Greedy, Array, Sorting and Heap (Priority Queue) on LeetCode.