Skip to main content

Minimum Amount of Time to Fill Cups - Solution & Explanation

EasyArrayGreedySortingHeap (Priority Queue)11 min readAsked at: Google
Practice this problem

Problem Statement

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.

 

Example 1:

Input: amount = [1,4,2]
Output: 4
Explanation: One way to fill up the cups is:
Second 1: Fill up a cold cup and a warm cup.
Second 2: Fill up a warm cup and a hot cup.
Second 3: Fill up a warm cup and a hot cup.
Second 4: Fill up a warm cup.
It can be proven that 4 is the minimum number of seconds needed.

Example 2:

Input: amount = [5,4,4]
Output: 7
Explanation: One way to fill up the cups is:
Second 1: Fill up a cold cup, and a hot cup.
Second 2: Fill up a cold cup, and a warm cup.
Second 3: Fill up a cold cup, and a warm cup.
Second 4: Fill up a warm cup, and a hot cup.
Second 5: Fill up a cold cup, and a hot cup.
Second 6: Fill up a cold cup, and a warm cup.
Second 7: Fill up a hot cup.

Example 3:

Input: amount = [5,0,0]
Output: 5
Explanation: Every second, we fill up a cold cup.

 

Constraints:

  • amount.length == 3
  • 0 <= amount[i] <= 100

Approach Overview

Problem Overview: You are given an array amount where each element represents the number of cold, warm, and hot cups to fill. Each second you can either fill two different types of cups or one cup of any type. The task is to compute the minimum number of seconds required to finish filling all cups.

Approach 1: Greedy Counting (O(1) time, O(1) space)

The key observation is that one second can process two different cup types simultaneously. The total work is the sum of all cups, but you cannot process more than one cup of the same type per second. Sort the three values or compute the maximum directly. The minimum time is max(max(amount), ceil(sum(amount)/2)). The first term handles the case where one type dominates, while the second term represents pairing cups whenever possible. This greedy reasoning avoids simulation and works in constant time because the array size is fixed. The approach relies on simple array manipulation and a classic greedy observation.

Approach 2: Priority Queue (Heap) Simulation (O(n log 3) time, O(1) space)

This method simulates the process using a max heap. Push the three cup counts into a priority queue. Each second, pop the two largest values, decrement both, and push them back if they are still positive. If only one type remains, decrement it alone. The heap ensures the two largest counts are always chosen, which maximizes parallel filling each second. Because the heap size never exceeds three elements, operations are cheap, but the loop runs for roughly the total number of seconds required.

Recommended for interviews: Interviewers usually expect the greedy observation. The heap simulation demonstrates understanding of priority queues, but the constant-time greedy formula shows stronger problem insight and leads to the most optimal solution.

Approach 1: Greedy Approach

The greedy approach involves selecting the largest possible numbers from the amount array to fill in pairs such that the number of seconds is minimized. By always aiming to fill two types of water at once, we can quickly reduce the total amount.

In this solution, we compute the sum and find the maximum of the given amounts. The minimum time to fill the cups is either half of the total amount rounded up or the maximum number in the array, whichever is greater. This is because the largest number cannot be exceeded by pairs of other numbers.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), since we only perform a few operations on a fixed-size array.
Space Complexity: O(1), as only a few extra variables are used.

Try this approach in the editor →

Approach 2: Priority Queue (Heap) Approach

This method uses a priority queue to always prioritize filling the largest cups first. By maintaining the heap order, we can dynamically get and reduce the largest values effectively.

Implementing a priority queue in C requires custom structures and functions to maintain the heap property, which can be tedious for a succinct example.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Implementation-dependent; typically O(log n) for heap operations in custom implementations.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Approach

Time Complexity: O(1), since we only perform a few operations on a fixed-size array.
Space Complexity: O(1), as only a few extra variables are used.

Priority Queue (Heap) Approach

Implementation-dependent; typically O(log n) for heap operations in custom implementations.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy FormulaO(1)O(1)Best solution when recognizing the pairing insight between cup types
Priority Queue (Heap) SimulationO(n log 3) ≈ O(n)O(1)Useful for understanding the process step-by-step or when extending to more cup types

Video Solution

Weekly Contest 301 | Leetcode 2335 Minimum Amount of Time to Fill Cups | Easy Heaps • Coding Decoded • 2,546 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Amount of Time to Fill Cups easy or hard?
Minimum Amount of Time to Fill Cups is classified as an Easy problem on LeetCode with an acceptance rate around 60%. The challenge lies in recognizing the greedy pairing insight rather than simulating every second.
Minimum Amount of Time to Fill Cups Python/Java solution
Most implementations compute the sum of the array and the maximum value, then return max(maxValue, (sum + 1) // 2). This works in Python, Java, C++, and JavaScript with only a few lines of code and constant time complexity.
How to solve Minimum Amount of Time to Fill Cups in O(n)?
A heap-based simulation achieves roughly O(n) time. Insert the three values into a max heap, repeatedly remove the two largest counts, decrement them, and push them back until all cups are filled. Since the heap size is constant (3), each operation is O(log 3).
What is the best approach for Minimum Amount of Time to Fill Cups?
The optimal approach is a greedy formula based on the total cups and the largest cup type. The answer is max(max(amount), ceil(sum(amount) / 2)). This works because each second can process at most two cups of different types. The algorithm runs in O(1) time and O(1) space.
Is Minimum Amount of Time to Fill Cups asked at Google/Amazon/Meta?
Greedy scheduling and heap simulation problems similar to this appear frequently in interviews at companies like Amazon, Google, and Meta. The exact problem may vary, but recognizing greedy pairing or using a priority queue to balance workloads is a common interview pattern.
What data structure is used in Minimum Amount of Time to Fill Cups?
Two main concepts appear in solutions: greedy reasoning and a priority queue (max heap). The greedy solution uses simple arithmetic with arrays, while the alternative simulation uses a heap to always select the two largest remaining cup counts.
What is the time complexity of Minimum Amount of Time to Fill Cups?
The optimal greedy solution runs in O(1) time because the array always contains exactly three values. It only computes the sum and the maximum element. A simulation using a heap runs in about O(n log 3), where n is the number of seconds simulated.

Ready to solve this problem?

Practice Minimum Amount of Time to Fill Cups with our built-in code editor and test cases.

Practice on FleetCode