-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
30 lines (29 loc) · 780 Bytes
/
Solution.cs
File metadata and controls
30 lines (29 loc) · 780 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
29
30
public class Solution
{
public double ChampagneTower(int poured, int query_row, int query_glass)
{
// 1
// 1 1
// 1 2 1
// 1 3 3 1
// 1 4 6 4 1
double[] row = new double[query_row + 1];
row[0] = poured;
for (int i = 1; i <= query_row; i++)
{
double[] nextRow = new double[query_row + 1];
// get poured
for (int j = 0; j <= i; j++)
{
row[j] = Math.Max(0.0, row[j] - 1.0) / 2.0;
}
nextRow[0] = row[0];
for (int j = 1; j <= i; j++)
{
nextRow[j] = row[j] + row[j - 1];
}
row = nextRow;
}
return Math.Min(1.0, row[query_glass]);
}
}