Skip to content

Commit cf931a7

Browse files
committed
Time: 9 ms (17.13%), Space: 43 MB (81.32%) - LeetHub
1 parent 070619e commit cf931a7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
3+
public double champagneTower(int poured, int query_row, int query_glass) {
4+
if (poured == 0)
5+
return 0;
6+
7+
List<Double> prevRow = new ArrayList<>(List.of((double) poured));
8+
9+
while (query_row-- > 0) {
10+
List<Double> currentRow = new ArrayList<Double>();
11+
double champagneInEnds = Math.max(0, (prevRow.get(0) - 1) / 2); // min champagne can be 0
12+
13+
currentRow.add(champagneInEnds); // first glass
14+
15+
for (int i = 1; i < prevRow.size(); i++)
16+
currentRow.add(Math.max(0, (prevRow.get(i - 1) - 1) / 2) + // flow from top-left glass
17+
Math.max(0, (prevRow.get(i) - 1) / 2)); // flow from top-right glass
18+
19+
currentRow.add(champagneInEnds); // last glass
20+
prevRow = currentRow;
21+
}
22+
23+
return Math.min(1, prevRow.get(query_glass)); // max champagne can be 1
24+
}
25+
}

0 commit comments

Comments
 (0)