-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchampagneTower.cpp
More file actions
43 lines (40 loc) · 1.22 KB
/
champagneTower.cpp
File metadata and controls
43 lines (40 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Source: https://leetcode.com/problems/champagne-tower/
// Author: Miao Zhang
// Date: 2021-03-11
class Solution {
public:
double champagneTower(int poured, int query_row, int query_glass) {
int n = 100;
vector<vector<double>> dp(n, vector<double>(n));
dp[0][0] = poured;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j <= i; j++) {
if (dp[i][j] > 1) {
dp[i + 1][j] += (dp[i][j] - 1) / 2.0;
dp[i + 1][j + 1] += (dp[i][j] - 1) / 2.0;
dp[i][j] = 1.0;
}
}
}
return std::min(1.0, dp[query_row][query_glass]);
}
};
class Solution {
public:
double champagneTower(int poured, int query_row, int query_glass) {
int n = 100;
vector<double> dp(n);
dp[0] = poured;
for (int i = 0; i < query_row; i++) {
vector<double> tmp(n);
for (int j = 0; j <= i; j++) {
if (dp[j] > 1) {
tmp[j] += (dp[j] - 1) / 2.0;
tmp[j + 1] += (dp[j] - 1) / 2.0;
}
}
dp.swap(tmp);
}
return std::min(1.0, dp[query_glass]);
}
};