8000 add 799 · yochju/Leetcode@c1e0462 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1e0462

Browse files
add 799
1 parent ffa9e52 commit c1
8000
e0462

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|799|[Champagne Tower](https://leetcode.com/problems/champagne-tower/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_799.java) | O(r^2) or O(1) | O(r^2) or O(1) | |Medium|
2526
|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | O(n) | O(1) | |Easy|
2627
|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | O(n+m) | O(1) | |Medium|
2728
|788|[Rotated Digits](https://leetcode.com/problems/rotated-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_788.java) | O(n*m) | O(1) | |Easy|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 799. Champagne Tower
5+
6+
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 (250ml) of champagne.
7+
Then, some champagne is poured in the first glass at the top.
8+
When the top most glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it.
9+
When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on.
10+
(A glass at the bottom row has it's excess champagne fall on the floor.)
11+
For example, after one cup of champagne is poured, the top most glass is full.
12+
After two cups of champagne are poured, the two glasses on the second row are half full.
13+
After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now.
14+
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.
15+
Now after pouring some non-negative integer cups of champagne, return how full the j-th glass in the i-th row is (both i and j are 0 indexed.)
16+
17+
Example 1:
18+
Input: poured = 1, query_glass = 1, query_row = 1
19+
Output: 0.0
20+
Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
21+
22+
Example 2:
23+
Input: poured = 2, query_glass = 1, query_row = 1
24+
Output: 0.5
25+
Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
26+
27+
Note:
28+
poured will be in the range of [0, 10 ^ 9].
29+
query_glass and query_row will be in the range of [0, 99].
30+
31+
*
32+
*/
33+
public class _799 {
34+
public static class Solution1 {
35+
public double champagneTower(int poured, int query_row, int query_glass) {
36+
double[][] dp = new double[101][101];
37+
dp[0][0] = poured;
38+
for (int row = 0; row <= query_row; row++) {
39+
for (int col = 0; col <= row; col++) {
40+
double quantity = (dp[row][col] - 1.0)/2.0;
41+
if (quantity > 0) {
42+
dp[row+1][col] += quantity;
43+
dp[row+1][col+1] += quantity;
44+
}
45+
}
46+
}
47+
return Math.min(dp[query_row][query_glass], 1.0);
48+
}
49+
}
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._799;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _799Test {
10+
private static _799.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _799.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(0.125, solution1.champagneTower(8, 3, 0), 0);
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(0.875, solution1.champagneTower(8, 3, 1), 0);
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0.875, solution1.champagneTower(8, 3, 2), 0);
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(0.125, solution1.champagneTower(8, 3, 3), 0);
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(0.0, solution1.champagneTower(1, 1, 1), 0);
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(0.5, solution1.champagneTower(2, 1, 1), 0);
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(0.0, solution1.champagneTower(1000000000, 99, 99), 0);
50+
}
51+
}

0 commit comments

Comments
 (0)
0