Skip to content

Commit 51603ec

Browse files
committed
feat: solve No.799
1 parent ba02cef commit 51603ec

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

701-800/799. Champagne Tower.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 799. Champagne Tower
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Dynamic Programming.
5+
- Similar Questions: Number of Ways to Build House of Cards.
6+
7+
## Problem
8+
9+
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.
10+
11+
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.)
12+
13+
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.
14+
15+
16+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png)
17+
18+
19+
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.)
20+
21+
 
22+
Example 1:
23+
24+
```
25+
Input: poured = 1, query_row = 1, query_glass = 1
26+
Output: 0.00000
27+
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.
28+
```
29+
30+
Example 2:
31+
32+
```
33+
Input: poured = 2, query_row = 1, query_glass = 1
34+
Output: 0.50000
35+
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.
36+
```
37+
38+
Example 3:
39+
40+
```
41+
Input: poured = 100000009, query_row = 33, query_glass = 17
42+
Output: 1.00000
43+
```
44+
45+
 
46+
**Constraints:**
47+
48+
49+
50+
- `0 <= poured <= 109`
51+
52+
- `0 <= query_glass <= query_row < 100`
53+
54+
55+
## Solution
56+
57+
```javascript
58+
/**
59+
* @param {number} poured
60+
* @param {number} query_row
61+
* @param {number} query_glass
62+
* @return {number}
63+
*/
64+
var champagneTower = function(poured, query_row, query_glass) {
65+
var glasses = [poured];
66+
for (var i = 0; i < query_row; i++) {
67+
var nextRow = Array(i + 2).fill(0);
68+
var hasNext = false;
69+
for (var j = 0; j <= i; j++) {
70+
var extra = (glasses[j] - 1) / 2;
71+
if (extra <= 0) continue;
72+
hasNext = true;
73+
nextRow[j] += extra;
74+
nextRow[j + 1] += extra;
75+
}
76+
if (!hasNext) return 0;
77+
glasses = nextRow;
78+
}
79+
return Math.min(glasses[query_glass], 1);
80+
};
81+
```
82+
83+
**Explain:**
84+
85+
nope.
86+
87+
**Complexity:**
88+
89+
* Time complexity : O(query_row ^ 2).
90+
* Space complexity : O(1).

0 commit comments

Comments
 (0)