Champagne Tower (LeetCode 799): Solution and Explanation
Glasses form a pyramid and each holds one cup. You pour some cups into the top glass, and whatever a full glass cannot hold splits evenly into the two glasses below it. Return how full one specific glass ends up, between 0 and 1.
Intuition
It is tempting to think in terms of how much each glass holds, but that number is capped at 1 and loses information. What we really need is how much liquid flowed into each glass in total. A glass that received x cups keeps min(1, x) and passes the rest on.
If a glass receives x > 1 cups, the extra x - 1 splits in half, so each child receives (x - 1) / 2 from it. A glass in the middle of a row has two parents, so it receives the sum of two such halves; a glass at the edge of a row has only one parent.
Liquid only ever moves down. Once we know the totals for row r, row r + 1 follows directly, and nothing later can change row r again. That gives a simple row-by-row DP, like building Pascal's triangle but with overflow instead of sums.
We only need rows down to query_row, and only the previous row at any moment, so the work is about query_row^2 / 2 updates and the memory is one row.
Approach: Row-by-row flow simulation
- Start with
row = [poured]: everything flows into the top glass. - For
rfrom 0 toquery_row - 1, createnxtwithr + 2zeros. - For each glass
ginrow, computeextra = (row[g] - 1) / 2. If it is positive, add it tonxt[g]andnxt[g + 1]. - Set
row = nxtand continue to the next row. - Return
min(1, row[query_glass]), because a glass can never hold more than one cup.
Step-by-step example
poured = 1, query_row = 1, query_glass = 1
- row = [1]: the single cup flows into the top glass.
- Row 0 to row 1: the top glass received exactly 1 cup, so extra = (1 - 1) / 2 = 0 and nothing flows down.
- Row 1 is [0, 0].
- Glass (1, 1) received 0 cups, so it is min(1, 0) = 0.0 full.
Code: Python, Java, C++
1class Solution:
2 def champagneTower(self, poured: int, query_row: int, query_glass: int) -> float:
3 row = [float(poured)]
4 for r in range(query_row):
5 nxt = [0.0] * (r + 2)
6 for g, amount in enumerate(row):
7 extra = (amount - 1) / 2
8 if extra > 0:
9 nxt[g] += extra
10 nxt[g + 1] += extra
11 row = nxt
12 return min(1.0, row[query_glass])1class Solution {
2 public double champagneTower(int poured, int query_row, int query_glass) {
3 double[] row = { poured };
4 for (int r = 0; r < query_row; r++) {
5 double[] nxt = new double[r + 2];
6 for (int g = 0; g < row.length; g++) {
7 double extra = (row[g] - 1) / 2;
8 if (extra > 0) {
9 nxt[g] += extra;
10 nxt[g + 1] += extra;
11 }
12 }
13 row = nxt;
14 }
15 return Math.min(1.0, row[query_glass]);
16 }
17}1class Solution {
2public:
3 double champagneTower(int poured, int query_row, int query_glass) {
4 vector<double> row = { (double)poured };
5 for (int r = 0; r < query_row; r++) {
6 vector<double> nxt(r + 2, 0.0);
7 for (int g = 0; g < (int)row.size(); g++) {
8 double extra = (row[g] - 1) / 2;
9 if (extra > 0) {
10 nxt[g] += extra;
11 nxt[g + 1] += extra;
12 }
13 }
14 row = nxt;
15 }
16 return min(1.0, row[query_glass]);
17 }
18};Time and space complexity
O(R^2)
R is query_row. Row r has r + 1 glasses and each is processed once, so the total is 1 + 2 + ... + R, which is O(R^2).
O(R)
Only the current row and the next one are stored.
Other approaches
Full 2D table (time O(R^2), space O(R^2))
Keep a 100 x 100 table where t[r][g] is the amount that flowed into glass (r, g) and fill it top-down with the same rule. It is easy to read and debug but uses O(R^2) memory.
Pour one cup at a time (time O(poured * R), space O(R^2))
Simulate each cup separately, letting it trickle down until it lands in a glass with room. It is intuitive but far too slow when poured reaches 10^9, and it also mis-models the even split.
Common mistakes
- Capping a glass at 1 before passing the overflow down. You must split
amount - 1, using the uncapped amount that flowed in. - Passing the whole overflow to each child instead of half to each.
- Returning the raw amount for the query glass. It can be far above 1, so take
min(1, amount). - Using integer division in languages like Java or C++. The amounts are fractional, so use doubles.
Frequently asked questions
Why track the amount that flowed in instead of what each glass holds?
What a glass holds is capped at 1, so it cannot tell you how much spills over. The total that flowed in determines both the fill level and the overflow.
Does the order in which we process glasses in a row matter?
No. Each glass in row r only adds to row r + 1, which we write into a separate array, so any order inside the row gives the same totals.
Can floating point errors break the answer?
The amounts are halved at each level, which doubles represent well. The judge also compares doubles numerically, so tiny rounding differences in the last digit are not a problem in practice.
Why is this considered dynamic programming?
Each glass's amount is computed from its two parents' amounts, which were already computed for the row above. That is the same overlapping-subproblem structure as Pascal's triangle.