Champagne Tower

Glasses are stacked in a pyramid: row 0 has one glass, row 1 has two, and so on up to row 99. Each glass holds one cup. You pour `poured` cups into the top glass. When a glass is full, any extra liquid splits equally and falls into the two glasses just below it (left and right). Return how full the glass at index `query_glass` in row `query_row` is (both 0-indexed), as a number between 0 and 1.

30:00

Champagne Tower
medium
Free written solution
Topics
Companies

Glasses are stacked in a pyramid: row 0 has one glass, row 1 has two, and so on up to row 99. Each glass holds one cup. You pour poured cups into the top glass. When a glass is full, any extra liquid splits equally and falls into the two glasses just below it (left and right). Return how full the glass at index query_glass in row query_row is (both 0-indexed), as a number between 0 and 1.

Example 1:
Input: {"poured":1,"query_row":1,"query_glass":1}
Output: 0.0
Constraints:
  • 0≤poured≤1090 \leq \text{poured} \leq 10^9

  • 0≤query_glass≤query_row<1000 \leq \text{query\_glass} \leq \text{query\_row} < 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"poured":1,"query_row":1,"query_glass":1}

poured

1

rows settled

0 / 1

glass (1, 1)

?

r0

r1

numbers = how full (max 1)┅ query glass

The tower is empty. We will track the total amount that flows into each glass, not just what it keeps, down to row 1.

Variables
VariableValue
poured1
query_row1
query_glass1
DepthFunction Call
Stack empty
0/4