Champagne Tower - Problem

We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100th row. Each glass holds one cup of champagne.

Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on.

For example, after one cup of champagne is poured, the topmost glass is full. After two cups of champagne are poured, the two glasses on the second row are half full. After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now.

Now after pouring some non-negative integer cups of champagne, return how full the jth glass in the ith row is (both i and j are 0-indexed).

Input & Output

Example 1 — Two Cups Poured
$ Input: poured = 2, query_row = 1, query_glass = 1
Output: 0.5
💡 Note: Pour 2 cups into top glass: glass[0][0] gets 1.0 (full), remaining 1.0 overflows equally to row 1. Each glass in row 1 gets 0.5.
Example 2 — One Cup Only
$ Input: poured = 1, query_row = 1, query_glass = 1
Output: 0.0
💡 Note: Only 1 cup poured fills the top glass completely. No overflow reaches row 1, so glass[1][1] remains empty.
Example 3 — Large Pour
$ Input: poured = 100000009, query_row = 33, query_glass = 17
Output: 1.0
💡 Note: With 100 million cups, champagne fills many levels completely. Glass at row 33, position 17 will be completely full.

Constraints

  • 0 ≤ poured ≤ 109
  • 0 ≤ query_glass ≤ query_row < 100

Visualization

Tap to expand
Champagne Tower INPUT 1.0 full 0.5 0.5 2 cups poured = 2 query_row = 1 query_glass = 1 ALGORITHM STEPS 1 Initialize DP Table dp[0][0] = poured = 2 2 Process Overflow If glass > 1, excess flows to left/right 3 Calculate Flow excess = (2-1)/2 = 0.5 each side gets 0.5 4 Return Result min(1, dp[row][glass]) DP Table State Row 0: 2.0 Row 1: 0.5 0.5 Row 2: 0 0 0 FINAL RESULT 50% Glass [1][1] Half Full OUTPUT 0.5 OK - Verified Correct Key Insight: Simulate champagne flow using dynamic programming. Each glass receives overflow from the glass above-left and above-right. Excess = max(0, current - 1), and half flows to each neighbor below. Time: O(row^2) | Space: O(row^2) - Process level by level until query_row is reached. TutorialsPoint - Champagne Tower | Optimal DP Solution
Asked in
Google 12 Facebook 8 Microsoft 6
125.0K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen