Skip to content

Commit d980150

Browse files
committed
https://leetcode.cn/problems/champagne-tower
1 parent bb55fa9 commit d980150

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Step 2. Add the dependency
4747

4848
https://leetcode.cn/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
4949

50+
https://leetcode.cn/problems/champagne-tower
51+
5052
https://leetcode.cn/problems/last-stone-weight-ii/
5153

5254
https://leetcode.cn/problems/delete-characters-to-make-fancy-string/

champagne-tower/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function champagneTower(
2+
poured: number,
3+
query_row: number,
4+
query_glass: number,
5+
): number {
6+
let row = [poured];
7+
for (let i = 1; i <= query_row; i++) {
8+
const nextRow: number[] = new Array(i + 1).fill(0);
9+
for (let j = 0; j < i; j++) {
10+
const volume = row[j];
11+
if (volume > 1) {
12+
nextRow[j] += (volume - 1) / 2;
13+
nextRow[j + 1] += (volume - 1) / 2;
14+
}
15+
}
16+
row = nextRow;
17+
}
18+
return Math.min(1, row[query_glass]);
19+
}

0 commit comments

Comments
 (0)