-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathChampagne Tower.cpp
107 lines (83 loc) · 4.08 KB
/
Champagne Tower.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Solution by Rahul Surana
***********************************************************
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.
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.)
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.
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.)
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
// vector<vector<double>> dp;
//
// **********************************
// Top Down Approach
// **********************************
// double df(int i, int j, int poured){
// if(j < 0 || j >= i+1 || i < 0) return 0.0;
// if(i == 0 && j == 0) return dp[i][j] = (double) poured;
// // if(i == 1) return dp[i][j] = (double)(poured-1)/2.0;
// if(dp[i][j] != -1.0) return dp[i][j];
// double y = df(i-1,j-1,poured);
// double z = df(i-1,j,poured);
// double x = (y>1? y-1: 0)/2 + (z>1?z-1:0)/2;
// cout << i << " "<< j<<" "<<x<<"\n";
// return dp[i][j] = x;
// }
double champagneTower(int poured, int query_row, int query_glass) {
//
// **********************************
// Top Down Approach
// **********************************
// dp.resize(query_row+2,vector<double>(query_glass+2,-1.0));
// double x = df(query_row,query_glass,poured);
// return x > 1 ? 1.0 : x;
//
// *******************************
// Bottom Up Approach
// *******************************
//
// dp[0][0] = (double)poured;
vector<double> dp(query_glass+2);
dp[0] = (double)poured;
for(int i = 1; i <= query_row; i++){
vector<double> temp(query_glass+2);
for(int j = 0; j <= query_glass ; j++){
//
// *******************************
// Bottom Up Approach
// *******************************
//
// if(j == 0){
// dp[i][j] = (dp[i-1][j] > 1 ? (dp[i-1][j] - 1) : 0) / 2;
// }
// else{
// dp[i][j] = (dp[i-1][j] > 1 ? (dp[i-1][j] - 1) : 0) / 2 + (dp[i-1][j-1] > 1 ? (dp[i-1][j-1] - 1) : 0) / 2;
// // else {
// // dp[i][j] = (dp[i-1][j] > 1 ? (dp[i-1][j] - 1) : 0) / 2 + (dp[i-1][j-1] > 1 ? (dp[i-1][j-1] - 1) : 0) / 2;
// // }
// }
if(j == 0){
temp[j] = (dp[j] > 1 ? (dp[j] - 1) : 0) / 2;
}
else{
temp[j] = (dp[j] > 1 ? (dp[j] - 1) : 0) / 2 + (dp[j-1] > 1 ? (dp[j-1] - 1) : 0) / 2;
}
}
dp = temp;
}
// return dp[query_row][query_glass] > 1 ? 1.0 : dp[query_row][query_glass];
return dp[query_glass] > 1?1.0: dp[query_glass];
}
};