Minimum Amount of Time to Fill Cups - 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
  • Fill up 1 cup of any type of water

You are given a 0-indexed integer array amount of length 3 where:

  • amount[0] = number of cold water cups needed
  • amount[1] = number of warm water cups needed
  • amount[2] = number of hot water cups needed

Return the minimum number of seconds needed to fill up all the cups.

Input & Output

Example 1 — Balanced Distribution
$ Input: amount = [1,4,2]
Output: 4
💡 Note: Optimal strategy: Fill warm+hot (1 sec) → [1,3,1], Fill warm+hot (1 sec) → [1,2,0], Fill cold+warm (1 sec) → [0,1,0], Fill warm (1 sec) → [0,0,0]. Total: 4 seconds.
Example 2 — One Dominant Type
$ Input: amount = [5,4,4]
Output: 7
💡 Note: With 5 cold cups, we can pair optimally until other types run out. Cold+warm and cold+hot simultaneously for 4 seconds, then 1 cold cup remains, taking 1 more second. Total: 5 + 2 = 7 seconds.
Example 3 — Equal Amounts
$ Input: amount = [5,0,0]
Output: 5
💡 Note: Only one type available, so we must fill one cup per second for 5 seconds. No pairing possible.

Constraints

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

Visualization

Tap to expand
Water Dispenser: Fill Cups Optimally142ColdWarmHotInput: [1,4,2]Fill 2 cupsdifferent typesper secondStrategy4SecondsPair largest amounts: (4,2) → (3,1) → (2,0) + (1,0) → (1,0) → (0,0)Optimal pairing minimizes total timeAnswer: 4 seconds
Understanding the Visualization
1
Input
Array of 3 cup counts: [cold, warm, hot]
2
Strategy
Fill 2 different types per second when possible
3
Output
Minimum seconds needed to fill all cups
Key Takeaway
🎯 Key Insight: Always pair the two largest cup counts to minimize total filling time
Asked in
Google 15 Microsoft 8 Amazon 12
32.0K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen