Skip to content

Commit 5e9c6d7

Browse files
committed
feat: add solutions to lc problem: No.0799
No.0799.Champagne Tower
1 parent 0fb11f3 commit 5e9c6d7

File tree

6 files changed

+221
-5
lines changed

6 files changed

+221
-5
lines changed

solution/0700-0799/0799.Champagne Tower/README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,103 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:模拟**
56+
57+
直接模拟倒香槟的过程,定义二维数组 $g$,初始时 `g[0][j]=poured`
58+
59+
对于每一层,如果当前杯子的香槟量 $g[i][j]$ 大于 $1$,香槟会向下一层的两个杯子倒入,倒入的量为 $\frac{g[i][j]-1}{2}$,即当前杯子的香槟量减去 $1$ 后除以 $2$,然后当前杯子的香槟量更新为 $1$。
60+
61+
最后返回 `g[query_row][query_glass]` 即可。
62+
5563
<!-- tabs:start -->
5664

5765
### **Python3**
5866

5967
<!-- 这里可写当前语言的特殊实现逻辑 -->
6068

6169
```python
62-
70+
class Solution:
71+
def champagneTower(self, poured: int, query_row: int, query_glass: int) -> float:
72+
g = [[0] * 110 for _ in range(110)]
73+
g[0][0] = poured
74+
for i in range(query_row + 1):
75+
for j in range(i + 1):
76+
if g[i][j] > 1:
77+
half = (g[i][j] - 1) / 2
78+
g[i][j] = 1
79+
g[i + 1][j] += half
80+
g[i + 1][j + 1] += half
81+
return g[query_row][query_glass]
6382
```
6483

6584
### **Java**
6685

6786
<!-- 这里可写当前语言的特殊实现逻辑 -->
6887

6988
```java
89+
class Solution {
90+
public double champagneTower(int poured, int query_row, int query_glass) {
91+
double[][] g = new double[110][110];
92+
g[0][0] = poured;
93+
for (int i = 0; i <= query_row; ++i) {
94+
for (int j = 0; j <= i; ++j) {
95+
if (g[i][j] > 1) {
96+
double half = (g[i][j] - 1) / 2.0;
97+
g[i][j] = 1;
98+
g[i + 1][j] += half;
99+
g[i + 1][j + 1] += half;
100+
}
101+
}
102+
}
103+
return g[query_row][query_glass];
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
double champagneTower(int poured, int query_row, int query_glass) {
114+
double g[110][110] = {0.0};
115+
g[0][0] = poured;
116+
for (int i = 0; i <= query_row; ++i) {
117+
for (int j = 0; j <= i; ++j) {
118+
if (g[i][j] > 1) {
119+
double half = (g[i][j] - 1) / 2.0;
120+
g[i][j] = 1;
121+
g[i + 1][j] += half;
122+
g[i + 1][j + 1] += half;
123+
}
124+
}
125+
}
126+
return g[query_row][query_glass];
127+
}
128+
};
129+
```
70130
131+
### **Go**
132+
133+
```go
134+
func champagneTower(poured int, query_row int, query_glass int) float64 {
135+
g := make([][]float64, 110)
136+
for i := range g {
137+
g[i] = make([]float64, 110)
138+
}
139+
g[0][0] = float64(poured)
140+
for i := 0; i <= query_row; i++ {
141+
for j := 0; j <= i; j++ {
142+
if g[i][j] > 1 {
143+
half := (g[i][j] - 1) / 2.0
144+
g[i][j] = 1
145+
g[i+1][j] += half
146+
g[i+1][j+1] += half
147+
}
148+
}
149+
}
150+
return g[query_row][query_glass]
151+
}
71152
```
72153

73154
### **...**

solution/0700-0799/0799.Champagne Tower/README_EN.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@
5555
<p><strong>Constraints:</strong></p>
5656

5757
<ul>
58-
5958
<li><code>0 &lt;=&nbsp;poured &lt;= 10<sup>9</sup></code></li>
60-
6159
<li><code>0 &lt;= query_glass &lt;= query_row&nbsp;&lt; 100</code></li>
62-
6360
</ul>
6461

6562
## Solutions
@@ -69,13 +66,86 @@
6966
### **Python3**
7067

7168
```python
72-
69+
class Solution:
70+
def champagneTower(self, poured: int, query_row: int, query_glass: int) -> float:
71+
g = [[0] * 110 for _ in range(110)]
72+
g[0][0] = poured
73+
for i in range(query_row + 1):
74+
for j in range(i + 1):
75+
if g[i][j] > 1:
76+
half = (g[i][j] - 1) / 2
77+
g[i][j] = 1
78+
g[i + 1][j] += half
79+
g[i + 1][j + 1] += half
80+
return g[query_row][query_glass]
7381
```
7482

7583
### **Java**
7684

7785
```java
86+
class Solution {
87+
public double champagneTower(int poured, int query_row, int query_glass) {
88+
double[][] g = new double[110][110];
89+
g[0][0] = poured;
90+
for (int i = 0; i <= query_row; ++i) {
91+
for (int j = 0; j <= i; ++j) {
92+
if (g[i][j] > 1) {
93+
double half = (g[i][j] - 1) / 2.0;
94+
g[i][j] = 1;
95+
g[i + 1][j] += half;
96+
g[i + 1][j + 1] += half;
97+
}
98+
}
99+
}
100+
return g[query_row][query_glass];
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
double champagneTower(int poured, int query_row, int query_glass) {
111+
double g[110][110] = {0.0};
112+
g[0][0] = poured;
113+
for (int i = 0; i <= query_row; ++i) {
114+
for (int j = 0; j <= i; ++j) {
115+
if (g[i][j] > 1) {
116+
double half = (g[i][j] - 1) / 2.0;
117+
g[i][j] = 1;
118+
g[i + 1][j] += half;
119+
g[i + 1][j + 1] += half;
120+
}
121+
}
122+
}
123+
return g[query_row][query_glass];
124+
}
125+
};
126+
```
78127
128+
### **Go**
129+
130+
```go
131+
func champagneTower(poured int, query_row int, query_glass int) float64 {
132+
g := make([][]float64, 110)
133+
for i := range g {
134+
g[i] = make([]float64, 110)
135+
}
136+
g[0][0] = float64(poured)
137+
for i := 0; i <= query_row; i++ {
138+
for j := 0; j <= i; j++ {
139+
if g[i][j] > 1 {
140+
half := (g[i][j] - 1) / 2.0
141+
g[i][j] = 1
142+
g[i+1][j] += half
143+
g[i+1][j+1] += half
144+
}
145+
}
146+
}
147+
return g[query_row][query_glass]
148+
}
79149
```
80150

81151
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
double champagneTower(int poured, int query_row, int query_glass) {
4+
double g[110][110] = {0.0};
5+
g[0][0] = poured;
6+
for (int i = 0; i <= query_row; ++i) {
7+
for (int j = 0; j <= i; ++j) {
8+
if (g[i][j] > 1) {
9+
double half = (g[i][j] - 1) / 2.0;
10+
g[i][j] = 1;
11+
g[i + 1][j] += half;
12+
g[i + 1][j + 1] += half;
13+
}
14+
}
15+
}
16+
return g[query_row][query_glass];
17+
}
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func champagneTower(poured int, query_row int, query_glass int) float64 {
2+
g := make([][]float64, 110)
3+
for i := range g {
4+
g[i] = make([]float64, 110)
5+
}
6+
g[0][0] = float64(poured)
7+
for i := 0; i <= query_row; i++ {
8+
for j := 0; j <= i; j++ {
9+
if g[i][j] > 1 {
10+
half := (g[i][j] - 1) / 2.0
11+
g[i][j] = 1
12+
g[i+1][j] += half
13+
g[i+1][j+1] += half
14+
}
15+
}
16+
}
17+
return g[query_row][query_glass]
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public double champagneTower(int poured, int query_row, int query_glass) {
3+
double[][] g = new double[110][110];
4+
g[0][0] = poured;
5+
for (int i = 0; i <= query_row; ++i) {
6+
for (int j = 0; j <= i; ++j) {
7+
if (g[i][j] > 1) {
8+
double half = (g[i][j] - 1) / 2.0;
9+
g[i][j] = 1;
10+
g[i + 1][j] += half;
11+
g[i + 1][j + 1] += half;
12+
}
13+
}
14+
}
15+
return g[query_row][query_glass];
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def champagneTower(self, poured: int, query_row: int, query_glass: int) -> float:
3+
g = [[0] * 110 for _ in range(110)]
4+
g[0][0] = poured
5+
for i in range(query_row + 1):
6+
for j in range(i + 1):
7+
if g[i][j] > 1:
8+
half = (g[i][j] - 1) / 2
9+
g[i][j] = 1
10+
g[i + 1][j] += half
11+
g[i + 1][j + 1] += half
12+
return g[query_row][query_glass]

0 commit comments

Comments
 (0)