Loading...

09-24 leetcode 0799

链接 799. Champagne Tower

题目

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,

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.)

题解

这题本质上是个模拟题,模拟完和1求最小就行

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def champagneTower(self, poured: int, query_row: int, query_glass: int) -> float:
dp = [[0 for _ in range(x)] for x in range(1, query_row + 2)]
dp[0][0] = poured
for i in range(0, query_row):
for j in range(len(dp[i])):
temp = (dp[i][j] - 1) / 2
if temp > 0:
dp[i + 1][j] += temp
dp[i + 1][j + 1] += temp
return min(1, dp[query_row][query_glass])


Comment