Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 == 30 <= amount[i] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,4,2]
[5,4,4]
[5,0,0]
construct-target-array-with-multiple-sums)maximum-score-from-removing-stones)maximum-running-time-of-n-computers)minimum-cost-to-make-array-equal)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2335: Minimum Amount of Time to Fill Cups
class Solution {
public int fillCups(int[] amount) {
int ans = 0;
while (amount[0] + amount[1] + amount[2] > 0) {
Arrays.sort(amount);
++ans;
amount[2]--;
amount[1] = Math.max(0, amount[1] - 1);
}
return ans;
}
}
// Accepted solution for LeetCode #2335: Minimum Amount of Time to Fill Cups
func fillCups(amount []int) int {
ans := 0
for amount[0]+amount[1]+amount[2] > 0 {
sort.Ints(amount)
ans++
amount[2]--
if amount[1] > 0 {
amount[1]--
}
}
return ans
}
# Accepted solution for LeetCode #2335: Minimum Amount of Time to Fill Cups
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
// Accepted solution for LeetCode #2335: Minimum Amount of Time to Fill Cups
impl Solution {
pub fn fill_cups(mut amount: Vec<i32>) -> i32 {
amount.sort();
let dif = amount[0] + amount[1] - amount[2];
if dif <= 0 {
return amount[2];
}
(dif + 1) / 2 + amount[2]
}
}
// Accepted solution for LeetCode #2335: Minimum Amount of Time to Fill Cups
function fillCups(amount: number[]): number {
amount.sort((a, b) => a - b);
let [a, b, c] = amount;
let diff = a + b - c;
if (diff <= 0) return c;
else return Math.floor((diff + 1) / 2) + c;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.