Minimum Amount of Time to Fill Cups — LeetCode 2335 Python Solution
EasyGreedyArraySortingHeap (Priority Queue)
- Problem
- #2335
- Pattern
- 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n log n) |
| Space | O(1) to O(n) auxiliary |
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
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.