package leetcode;

/**
 * https://leetcode.com/problems/champagne-tower/
 */
public class Problem799 {
    public double champagneTower(int poured, int query_row, int query_glass) {
        double[][] glasses = new double[100][100];
        int row = 0;
        glasses[row] = new double[]{poured};
        while (row < glasses.length) {
            int colIdx = 0;
            boolean overflow = false;
            for (int col = 0; col < glasses[row].length; col++) {
                double val = glasses[row][col];
                if (val == 0) {
                    break;
                }
                if (val > 1) {
                    glasses[row][col] = 1;
                    double half = (val - 1) / 2;
                    glasses[row + 1][colIdx++] += half;
                    glasses[row + 1][colIdx] += half;
                    overflow = true;
                }
            }
            if (!overflow) {
                break;
            }
            row++;
        }
        return glasses[query_row][query_glass];
    }

    public static void main(String[] args) {
        Problem799 prob = new Problem799();
//        System.out.println(prob.champagneTower(1, 1, 1)); // 0.0
//        System.out.println(prob.champagneTower(2, 1, 1)); // 0.5
//        System.out.println(prob.champagneTower(3, 0, 0)); // 1.0
//        System.out.println(prob.champagneTower(3, 1, 1)); // 1.0
//        System.out.println(prob.champagneTower(3, 2, 0)); // 0.0
//        System.out.println(prob.champagneTower(3, 2, 1)); // 0.0
//        System.out.println(prob.champagneTower(4, 2, 0)); // 0.25
//        System.out.println(prob.champagneTower(4, 1, 0)); // 1.0
//        System.out.println(prob.champagneTower(4, 1, 1)); // 1.0
//        System.out.println(prob.champagneTower(4, 2, 1)); // 0.5
        System.out.println(prob.champagneTower(6, 3, 0)); // 0.0
    }
}

@@ -230,29 +230,8 @@
 0%5D;%0A
-        int row = 0;%0A
     
@@ -242,19 +242,17 @@
 glasses%5B
-row
+0
 %5D = new 
@@ -281,104 +281,52 @@
     
-while (row %3C glasses.length) %7B%0A            int colIdx = 0;%0A            boolean overflow = false;
+for (int row = 0; row %3C= query_row; row++) %7B
 %0A   
@@ -440,85 +440,8 @@
 l%5D;%0A
-                if (val == 0) %7B%0A                    break;%0A                %7D%0A
     
@@ -603,13 +603,8 @@
 %5Bcol
-Idx++
 %5D +=
@@ -654,11 +654,12 @@
 %5Bcol
-Idx
+ + 1
 %5D +=
@@ -669,45 +669,8 @@
 lf;%0A
-                    overflow = true;%0A
     
@@ -709,974 +709,70 @@
     
-    if (!overflow) %7B%0A                break;%0A            %7D%0A            row++;%0A        %7D%0A        return glasses%5Bquery_row%5D%5Bquery_glass%5D;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem799 prob = new Problem799();%0A//        System.out.println(prob.champagneTower(1, 1, 1)); // 0.0%0A//        System.out.println(prob.champagneTower(2, 1, 1)); // 0.5%0A//        System.out.println(prob.champagneTower(3, 0, 0)); // 1.0%0A//        System.out.println(prob.champagneTower(3, 1, 1)); // 1.0%0A//        System.out.println(prob.champagneTower(3, 2, 0)); // 0.0%0A//        System.out.println(prob.champagneTower(3, 2, 1)); // 0.0%0A//        System.out.println(prob.champagneTower(4, 2, 0)); // 0.25%0A//        System.out.println(prob.champagneTower(4, 1, 0)); // 1.0%0A//        System.out.println(prob.champagneTower(4, 1, 1)); // 1.0%0A//        System.out.println(prob.champagneTower(4, 2, 1)); // 0.5%0A        System.out.println(prob.champagneTower(6, 3, 0)); // 0.0
+%7D%0A        return Math.min(1, glasses%5Bquery_row%5D%5Bquery_glass%5D);
 %0A   

Solve problem 799