LeetCode 799 | Champagne Tower | Java | Medium | 2D D P & Cascade Simulation | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 799 β Champagne Tower using Dynamic Programming, Simulation, and Math in Java.
We are given a pyramid of glasses where the first row contains 1 glass, the second row contains 2 glasses, and each glass holds up to 1 cup of liquid. When champagne is poured into the topmost glass (0, 0), any excess liquid spills equally into the two adjacent glasses directly below it at row r + 1. We must determine how full the glass at index query_glass in query_row is after pouring a total of poured cups of champagne.
By modeling the downward liquid flow using a 2D dynamic programming grid, we can track the total volume of liquid that reaches each glass. For any glass receiving more than 1.0 cup, the overflowing liquid is calculated as (tower[r][c] - 1.0) / 2.0 and distributed evenly to its left child (r + 1, c) and right child (r + 1, c + 1). Because query_row is strictly less than 100, simulating up to query_row requires at most 5,050 constant-time transitions, yielding an optimal O(R^2) time complexity and O(R^2) auxiliary space that runs in under a millisecond.
π₯ Problem Highlights
β Pyramid cascade modeling via state transition
β Overflow recurrence: (tower[r][c] - 1.0) / 2.0 distributed equally downwards
β Bottom-up row-by-row simulation up to query_row
β Safe clamping min(1.0, tower[query_row][query_glass])
β Optimal O(R^2) time complexity with at most 5,050 evaluations
β Clean double grid implementation preventing floating-point drift
β±οΈ TIMESTAMPS
00:00 Intro
01:06 Problem Understanding & Constraints
02:19 Pascal's Triangle Fluid Simulation Insight
03:50 2D Dynamic Programming Cascading Strategy
05:15 Java Code Walkthrough Line by Line
06:39 Test Cases Walkthrough & Simulation
08:08 Time Complexity Analysis (O(query_rowΒ²))
09:10 Space Complexity Analysis (O(query_rowΒ²))
09:56 Outro & Wrap-up
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/14-February/799-Champagne-Tower
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Dynamic Programming on Pyramids
Simulation Algorithms
Cascade Overflow Problems
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why does a glass at position (r, c) contribute to both (r + 1, c) and (r + 1, c + 1) equally?
1οΈβ£ Because gravity always pulls champagne towards the left
2οΈβ£ Because each glass is positioned directly atop the gap between two glasses in the row below, splitting overflow symmetrically in half
3οΈβ£ Because odd-indexed glasses cannot hold liquid
4οΈβ£ Because the capacity of each glass doubles on every successive row
#LeetCode799 #LeetCode #ChampagneTower #Java #JavaDSA #DynamicProgramming #Simulation #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 799 β Champagne Tower using Dynamic Programming, Simulation, and Math in Java.
We are given a pyramid of glasses where the first row contains 1 glass, the second row contains 2 glasses, and each glass holds up to 1 cup of liquid. When champagne is poured into the topmost glass (0, 0), any excess liquid spills equally into the two adjacent glasses directly below it at row r + 1. We must determine how full the glass at index query_glass in query_row is after pouring a total of poured cups of champagne.
By modeling the downward liquid flow using a 2D dynamic programming grid, we can track the total volume of liquid that reaches each glass. For any glass receiving more than 1.0 cup, the overflowing liquid is calculated as (tower[r][c] - 1.0) / 2.0 and distributed evenly to its left child (r + 1, c) and right child (r + 1, c + 1). Because query_row is strictly less than 100, simulating up to query_row requires at most 5,050 constant-time transitions, yielding an optimal O(R^2) time complexity and O(R^2) auxiliary space that runs in under a millisecond.
π₯ Problem Highlights
β Pyramid cascade modeling via state transition
β Overflow recurrence: (tower[r][c] - 1.0) / 2.0 distributed equally downwards
β Bottom-up row-by-row simulation up to query_row
β Safe clamping min(1.0, tower[query_row][query_glass])
β Optimal O(R^2) time complexity with at most 5,050 evaluations
β Clean double grid implementation preventing floating-point drift
β±οΈ TIMESTAMPS
00:00 Intro
01:06 Problem Understanding & Constraints
02:19 Pascal's Triangle Fluid Simulation Insight
03:50 2D Dynamic Programming Cascading Strategy
05:15 Java Code Walkthrough Line by Line
06:39 Test Cases Walkthrough & Simulation
08:08 Time Complexity Analysis (O(query_rowΒ²))
09:10 Space Complexity Analysis (O(query_rowΒ²))
09:56 Outro & Wrap-up
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/14-February/799-Champagne-Tower
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/14-February/799-Champagne-Tower/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Dynamic Programming on Pyramids
Simulation Algorithms
Cascade Overflow Problems
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why does a glass at position (r, c) contribute to both (r + 1, c) and (r + 1, c + 1) equally?
1οΈβ£ Because gravity always pulls champagne towards the left
2οΈβ£ Because each glass is positioned directly atop the gap between two glasses in the row below, splitting overflow symmetrically in half
3οΈβ£ Because odd-indexed glasses cannot hold liquid
4οΈβ£ Because the capacity of each glass doubles on every successive row
#LeetCode799 #LeetCode #ChampagneTower #Java #JavaDSA #DynamicProgramming #Simulation #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




