We are given a champagne tower with an infinite number of rows, where the first row has 1 glass, the second row has 2 glasses, the third row has 3 glasses, and so on. We pour poured
amount of champagne into the top glass. When a glass becomes full (it cannot hold more than 1 unit of champagne), the excess champagne spills equally to the two glasses immediately below it.
We need to find how much champagne is in the glass located at the query_row
and query_glass
.
poured
, query_row
, and query_glass
?
poured
can be up to 10^9, and query_row
, query_glass
up to 99 or similar.query_row
and query_glass
?
poured
champagne.query_row
.query_glass
will be the minimum of 1 and the amount (since a glass can’t have more than 1 unit of champagne).We simulate the pouring process until we reach the desired row and glass position.
def champagneTower(poured: int, query_row: int, query_glass: int) -> float:
# Initialize the first element with the poured champagne
towers = [[0] * (r + 1) for r in range(query_row + 1)]
towers[0][0] = poured
for r in range(query_row):
for c in range(r + 1):
overflow = (towers[r][c] - 1.0) / 2.0
if overflow > 0:
towers[r + 1][c] += overflow
towers[r + 1][c + 1] += overflow
return min(1, towers[query_row][query_glass])
# Example usage:
poured = 25
query_row = 6
query_glass = 1
print(champagneTower(poured, query_row, query_glass)) # Expected output depends on the simulation
query_row
.Thus, the overall time complexity is (O(R^2)), which is efficient for input constraints typical of this problem (e.g., (R \leq 99)).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?