Minimum Amount of Time to Fill Cups - Problem

You're operating a smart water dispenser at a busy office that can dispense cold, warm, and hot water. The dispenser has an intelligent optimization feature that allows you to:

  • Fill 2 cups simultaneously if they require different types of water
  • Fill 1 cup of any type if you can't fill two different types

Given an 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

Your goal is to find the minimum number of seconds needed to fill all cups efficiently.

Example: If you need [1, 4, 2] cups, you can fill warm+hot simultaneously for 2 seconds, then warm+cold for 1 second, leaving 2 warm cups that take 2 more seconds. Total: 5 seconds.

Input & Output

example_1.py β€” Balanced Distribution
$ Input: [1, 4, 2]
β€Ί Output: 4
πŸ’‘ Note: We can fill optimally: Second 1-2: fill warm+hot simultaneously (leaving [1,2,0]). Second 3: fill warm+cold (leaving [0,1,0]). Second 4: fill remaining warm cup. Total: 4 seconds.
example_2.py β€” Perfect Pairing
$ Input: [5, 4, 4]
β€Ί Output: 7
πŸ’‘ Note: Total = 13 cups. We can pair different types for 4 seconds (filling 8 cups), then need 5 more seconds for remaining cold cups. max(5, ⌈13/2βŒ‰) = max(5, 7) = 7 seconds.
example_3.py β€” Single Type Dominates
$ Input: [5, 0, 1]
β€Ί Output: 5
πŸ’‘ Note: Hot and cold can be paired once (1 second), but then we need 4 more seconds for the remaining cold cups. The cold requirement dominates, so answer = max(5, ⌈6/2βŒ‰) = 5 seconds.

Constraints

  • amount.length == 3
  • 0 ≀ amount[i] ≀ 100
  • At least one element in amount is strictly greater than 0

Visualization

Tap to expand
Optimal Cup Filling StrategyColdWarmHotPair Different TypesFormulamin_time = max( largest_single, ⌈total/2βŒ‰)Example: [1, 4, 2] β†’ max(4, ⌈7/2βŒ‰) = max(4, 4) = 4 seconds🎯 Key InsightAlways try to pair different types, but single dominant typebecomes bottleneck when it exceeds sum of others
Understanding the Visualization
1
Analyze Requirements
Identify the largest single requirement and total cups needed
2
Apply Strategy
Use greedy pairing when possible, single filling when necessary
3
Calculate Minimum
The answer is the maximum of bottleneck time vs optimal pairing time
Key Takeaway
🎯 Key Insight: The optimal strategy balances between maximizing simultaneous filling (2 cups/second) and recognizing when a single type becomes the limiting bottleneck.
Asked in
Google 42 Amazon 35 Microsoft 28 Meta 22
52.1K Views
Medium Frequency
~15 min Avg. Time
1.8K 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