forked from PRAN20/Top-Interview-Questions-LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChampaign-Tower.cpp
More file actions
28 lines (25 loc) · 792 Bytes
/
Champaign-Tower.cpp
File metadata and controls
28 lines (25 loc) · 792 Bytes
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
class Solution {
public:
double champagneTower(int poured, int query_row, int query_glass) {
//dp matrix
double v[101][101] = {0.0};
// as first glass will be poured always
// ans overflow will be flowed to next level
v[0][0] = poured;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j <= i; j++)
{
// If the glass >=1
if (v[i][j] >= 1)
{
// split (v[i][j] - 1) into next level
v[i + 1][j] += (v[i][j] - 1) / 2.0;
v[i + 1][j + 1] += (v[i][j] - 1) / 2.0;
v[i][j] = 1;
}
}
}
return v[query_row][query_glass];
}
};