#799Champagne Tower
Medium
→
Approach: Simulation
Intuition
In this problem, instead of simulating the final amount of champagne in each glass, we focus on the amount of champagne that flows through each glass. Any amount exceeding one unit is considered excess and is split equally between the two glasses in the row below (bottom-left and bottom-right). For example, if 10 units are poured into the top cup, 1 unit is retained and the remaining 9 units are distributed—4.5 units to each glass directly below.
Implementation
| |
Complexity Analysis
- Time complexity: $O(R^2)$ where $R$ is the
query_row. We iterater through each glass in every row up to the target row. - Space complexity: $O(R)$ for the
memoarray to store the current row’s flow.