Champagne Tower
Real-world version: How Full One Glass in the Stack GetsThe statement here is our own: the same inputs, outputs and technique.
Glasses are stacked in rows: one on the top row, two on the row below, three on the row below that, and so on. Every glass holds one measure when full.
Each glass rests so that anything overflowing it is shared out evenly between the two glasses directly beneath, one to its left and one to its right. Overflow running off either end of a row spills to the floor and is gone.
poured measures are poured into the top glass. Once everything has settled, return how full the glass sitting at position glass along row row is, counting the top glass as row 0 and counting the glasses along each row from 0.
Examples
Example 1
- Input
- poured = 2, row = 1, glass = 1
- Output
- 0.5
Two measures fill the top glass and leave one over, which shares evenly between the two glasses beneath, so each of them holds half a measure.
Example 2
- Input
- poured = 1, row = 1, glass = 0
- Output
- 0.0
One measure fills the top glass exactly, with nothing left to overflow, so the row below stays dry.
Example 3
- Input
- poured = 987654321, row = 33, glass = 17
- Output
- 1.0
The first thirty-four rows hold five hundred and ninety-five measures between them, and far more than that is poured, so every glass in them is full.
Constraints
- 0 <= poured <= 10^9
- 0 <= row <= 99
- 0 <= glass <= row
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def champagne_tower(poured: int, row: int, glass: int) -> float:public double champagneTower(int poured, int row, int glass)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 16 test cases: 16 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's