799. Champagne Tower

Link

Solution

Focus on how much champagne flows into the glass, and will overflow to the glasses on next level.

    public double champagneTower(int poured, int query_row, int query_glass) {
        double[][] flows = new double[101][101];
        flows[0][0] = poured;
        for(int i = 0; i < query_row; i++){
            for(int j = 0; j <= i; j++){
                if(flows[i][j] > 1){
                    double flowToNextLevel =(flows[i][j] - 1 )/ 2;
                    flows[i+1][j] += flowToNextLevel;
                    flows[i+1][j+1] += flowToNextLevel;
                }
            }
        }
        return Math.min(flows[query_row][query_glass], 1);
    }

Last updated

Was this helpful?