forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
46 lines (37 loc) · 1.23 KB
/
main.cpp
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
44
45
46
/// Source : https://leetcode.com/problems/champagne-tower/description/
/// Author : liuyubobobo
/// Time : 2018-03-11
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Recurrence
/// Time Complexity: O(max_row^2)
/// Space Complexity: O(max_row^2)
class Solution {
public:
double champagneTower(int poured, int query_row, int query_glass) {
vector<vector<double>> tower;
for(int i = 0; i < 100; i ++)
tower.push_back(vector<double>(i + 1, 0.0));
tower[0][0] = poured;
for(int i = 0 ; i < tower.size() ; i ++)
for(int j = 0 ; j <= i ; j ++){
if(tower[i][j] > 1.0){
double left = tower[i][j] - 1.0;
tower[i][j] = 1.0;
if(i + 1 < tower.size()){
tower[i + 1][j] += left / 2;
tower[i + 1][j + 1] += left / 2;
}
}
}
return tower[query_row][query_glass];
}
};
int main() {
cout << Solution().champagneTower(1, 1, 1) << endl; // 0.0
cout << Solution().champagneTower(2, 1, 1) << endl; // 0.5
cout << Solution().champagneTower(4, 1, 0) << endl; // 1
return 0;
}