All problems
1017MediumDynamic Programming

How Full One Glass in the Stack Gets

Tracked in this browser only
Write code

Trains the technique from

LeetCode 799Champagne Tower

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def champagne_tower(poured: int, row: int, glass: int) -> float:
Java
public double champagneTower(int poured, int row, int glass)
September 7
Apply