Minimum Amount of Time to Fill Cups — LeetCode 2335 Python Solution

EasyGreedyArraySortingHeap (Priority Queue)
Problem
#2335
Reading time
2 min

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 ans

Complexity

MeasureComplexity
TimeO(n log n)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview