Champagne Tower LeetCode solution in Python

1
# Runtime: 392 ms (Top 9.30%) | Memory: 14.4 MB (Top 11.38%)
2
class Solution:
3
def champagneTower(self, poured: int, r: int, c: int) -> float:
4
quantity = defaultdict(int)
5
quantity[(0, 0)] = poured
6
for i in range(r + 1):
7
flag = False
8
for j in range(i + 1):
9
prev_flow = quantity[(i, j)] - 1
10
if prev_flow <= 0:
11
continue
12
flag = True
13
quantity[(i, j)] = 1
14
quantity[(i + 1, j)] += prev_flow / 2
15
quantity[(i + 1, j + 1)] += prev_flow / 2
16
if not flag:
17
break
18
return quantity[(r, c)]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0