Champagne Tower - Complete Solution Guide
Champagne Tower is LeetCode problem 799, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100 th 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. (A glass at the bott
Detailed Explanation
The Champagne Tower problem simulates pouring champagne into a stack of glasses arranged in a pyramid. The goal is to determine how full a specific glass in the tower is after pouring a given amount of champagne into the topmost glass. The champagne flows downwards, and any excess in a glass is split equally between the two glasses directly beneath it. We are given the amount of champagne poured (`poured`), the row index (`query_row`), and the glass index (`query_glass`), and we need to return the amount of champagne in that glass, capped at 1.0 (since a glass can only be full).
Solution Approach
The solution uses dynamic programming to simulate the flow of champagne down the tower. It maintains an array `row_amounts` representing the amount of champagne in each glass of the current row. The algorithm iterates from row 0 up to `query_row`, calculating the champagne levels for each row. For each glass in a row, if it contains more than 1.0 unit of champagne, the excess amount is split equally between the two glasses in the next row directly below it. The `row_amounts` array is updated for each row, and finally, the amount of champagne in the target glass (`query_glass`) is returned, ensuring it's capped at 1.0.
Step-by-Step Algorithm
- Step 1: Initialize `row_amounts` with the amount of champagne poured into the top glass (row 0, glass 0).
- Step 2: Iterate from row 0 up to `query_row` - 1. In each iteration, calculate the champagne levels for the next row.
- Step 3: For each glass in the current row, check if it contains more than 1.0 unit of champagne. If it does, calculate the excess amount.
- Step 4: Distribute the excess champagne equally (excess / 2.0) to the two glasses in the next row directly below the current glass.
- Step 5: Update the `row_amounts` array with the calculated champagne levels for the next row.
- Step 6: After the loop finishes, return the amount of champagne in the glass at `query_row` and `query_glass`, ensuring the result is capped at 1.0.
Key Insights
- Insight 1: The problem can be solved using dynamic programming by simulating the champagne flow row by row.
- Insight 2: We only need to keep track of the current row's champagne levels to calculate the next row's levels, allowing for space optimization.
- Insight 3: It's important to cap the amount of champagne in each glass at 1.0, representing a full glass.
Complexity Analysis
Time Complexity: O(query_row^2)
Space Complexity: O(query_row)
Topics
This problem involves: Dynamic Programming.
Companies
Asked at: National Instruments.