#799
Champagne Tower
specialist · 650 · lc medium +30 · verified · 64.1% accepted · 4,150 likes · top 67%
Description
Glasses are stacked in a pyramid: row 1 has 1 glass, row 2 has 2 glasses, and so on up to row 100. Each glass holds exactly one cup of champagne.
Champagne is poured into the single glass at the top. Whenever a glass fills and overflows, the excess splits evenly between the two glasses directly below it. The same cascading process continues downward. (Any overflow from the bottom row spills onto the floor.)
After pouring some non-negative number of cups, return how full the jth glass in the ith row is (both i and j are 0-indexed).
Example 1:
Input: poured = 1, query_row = 1, query_glass = 1
Output: 0.00000
Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
Example 2:
Input: poured = 2, query_row = 1, query_glass = 1
Output: 0.50000
Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
Example 3:
Input: poured = 100000009, query_row = 33, query_glass = 17
Output: 1.00000
Code
1
2
3