File: minimum-amount-of-time-to-fill-cups/solution.py

Date: 2026-06-06

Time: 17:52

Explanation

Purpose

This file solves LeetCode 2335: Minimum Amount of Time to Fill Cups. A water dispenser can fill at most 2 cups of different types per second (or 1 cup of any type). Given three cup counts, find the minimum seconds to fill all of them.

Key Components

min_seconds(amount: list[int]) -> int — The sole function. It computes the answer as a closed-form expression rather than simulating the process:


return max(max(amount), (sum(amount) + 1) // 2)

This takes the maximum of two lower bounds:

1. max(amount) — The dominant-type bound. Even if you pair the largest type with a different type every second, you still need at least this many seconds.

2. (sum(amount) + 1) // 2 — The throughput bound. You fill at most 2 cups per second, so you need at least ceil(total / 2) seconds. The +1 with integer division implements ceiling without floating point.

Patterns

Closed-form over simulation. Many greedy solutions to this problem sort and re-pick the two largest counts each round (O(n * max) with a heap). This solution recognizes that both lower bounds are always *achievable* — you can always construct a valid filling schedule that hits whichever bound is tighter — so it collapses to O(1).

Ceiling division idiom. (n + d - 1) // d is the standard Python integer ceiling division pattern. Here d = 2, so it's (sum + 1) // 2.

Dependencies

Flow

1. Compute max(amount) — the single largest cup count.

2. Compute (sum(amount) + 1) // 2 — ceiling of half the total.

3. Return whichever is larger.

No loops, no branching, no mutation.

Invariants

Error Handling

None. An empty list would raise on max(amount). Negative values would produce a mathematically valid but semantically meaningless result. Both are outside the problem's constraint space.

Why the formula is correct

The key insight is a proof sketch: if max(amount) >= sumofothertwo, the largest type dominates and you pair it with the others until they run out, then fill the rest alone — exactly max(amount) seconds. If max(amount) < sumofothertwo, you can always interleave the three types to fill 2 cups every second (except possibly the last), giving ceil(total / 2). In neither case can you do better, so these bounds are tight.

Topics to Explore

Beliefs