Medium, Dynamic Programming
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. (A glass at the bottom row has its excess champagne fall on the floor.)
For example, after one cup of champagne is poured, the top most 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. After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.
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.)
Example 1:
Input: poured = 1, query_row = 1, query_glass = 1
Output: 0.00000
Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
Example 2:
Input: poured = 2, query_row = 1, query_glass = 1
Output: 0.50000
Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
Example 3:
Input: poured = 100000009, query_row = 33, query_glass = 17
Output: 1.00000
Constraints:
- 0 <= poured <= 109
- 0 <= query_glass <= query_row < 100
SOLUTION
To solve the Champagne Tower problem, we need to simulate the flow of champagne through the glasses in the pyramid. Here's how we can approach the solution:
Plan:
-
Understand the Flow:
- Each glass can hold up to 1 cup of champagne.
- Any excess champagne is split equally between the two glasses directly below it in the next row.
- A glass does not receive champagne from glasses other than the one(s) directly above it.
-
Simulation Using a 2D Array:
- Use a 2D array
tower
wheretower[i][j]
represents the amount of champagne in thej-th
glass of thei-th
row. - Start with
tower[0][0] = poured
. - Iterate row by row, calculating the overflow for each glass and distributing it to the glasses in the next row.
- Use a 2D array
-
Return the Result:
- If the champagne in the target glass (
tower[query_row][query_glass]
) is less than or equal to 1, return it. - Otherwise, return 1, as a glass can only be full.
- If the champagne in the target glass (
Implementation:
public class Solution {
public double ChampagneTower(int poured, int query_row, int query_glass) {
// Initialize a 2D array to simulate the champagne tower
double[][] tower = new double[query_row + 2][];
for (int i = 0; i < tower.Length; i++) {
tower[i] = new double[i + 1];
}
// Pour champagne into the topmost glass
tower[0][0] = poured;
// Simulate the champagne flow row by row
for (int row = 0; row <= query_row; row++) {
for (int col = 0; col <= row; col++) {
// If there is overflow
if (tower[row][col] > 1) {
double overflow = (tower[row][col] - 1) / 2.0;
tower[row + 1][col] += overflow; // Left glass in next row
tower[row + 1][col + 1] += overflow; // Right glass in next row
tower[row][col] = 1; // Current glass capped at 1
}
}
}
// Return the amount in the target glass
return Math.Min(1, tower[query_row][query_glass]);
}
public static void Main(string[] args) {
var solution = new Solution();
Console.WriteLine(solution.ChampagneTower(1, 1, 1)); // Output: 0.00000
Console.WriteLine(solution.ChampagneTower(2, 1, 1)); // Output: 0.50000
Console.WriteLine(solution.ChampagneTower(100000009, 33, 17)); // Output: 1.00000
}
}
Explanation of Code:
-
2D Array Initialization:
- The array
tower
is a jagged array, where each row can have a different number of glasses.
- The array
-
Flow Simulation:
- Start from the top row and move down.
- For each glass, if it contains more than 1 cup, calculate the overflow and distribute it equally to the two glasses below it.
- Cap the current glass's value at 1 since it cannot hold more than 1 cup.
-
Return the Result:
- The champagne amount in
tower[query_row][query_glass]
is capped at 1 if it's more than 1, as a glass cannot hold more than 1 cup.
- The champagne amount in
Complexity:
- Time Complexity:
- Each row processes all the glasses up to that row, resulting in a total of approximately operations.
- Space Complexity:
- We use a 2D array to store champagne levels for all glasses up to
query_row
.
- We use a 2D array to store champagne levels for all glasses up to
Example Outputs:
Example 1:
- Input:
poured = 1, query_row = 1, query_glass = 1
- Output:
0.00000
- Explanation: Only the top glass is full; no champagne flows to the second row.
Example 2:
- Input:
poured = 2, query_row = 1, query_glass = 1
- Output:
0.50000
- Explanation: The top glass overflows, and the two glasses in the second row each get 0.5 cups.
Example 3:
- Input:
poured = 100000009, query_row = 33, query_glass = 17
- Output:
1.00000
- Explanation: The glass is completely full due to the large amount of champagne poured.