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.&nbsp; Each glass holds one cup&nbsp;of champagne.

Then, some champagne is poured into the first glass at the top.&nbsp; When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it.&nbsp; When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on.&nbsp; (A glass at the bottom row has its excess champagne fall on the floor.)

For example, after one cup of champagne is poured, the top most glass is full.&nbsp; After two cups of champagne are poured, the two glasses on the second row are half full.&nbsp; After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now.&nbsp; 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.



Now after pouring some non-negative integer cups of champagne, return how full the jth glass in the ith row is (both i and j are 0-indexed.)

&nbsp;
Example 1:

Input: poured = 1, query_row = 1, query_glass = 1
Output: 0.00000
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.


Example 2:

Input: poured = 2, query_row = 1, query_glass = 1
Output: 0.50000
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.


Example 3:

Input: poured = 100000009, query_row = 33, query_glass = 17
Output: 1.00000


&nbsp;
Constraints:


	0 &lt;=&nbsp;poured &lt;= 109
	0 &lt;= query_glass &lt;= query_row&nbsp;&lt; 100

class Solution:
    def champagneTower(self, poured: int, r: int, c: int) -> float:
        quantity=defaultdict(int)
        quantity[(0,0)]=poured
        for i in range(r+1):
            flag=False
            for j in range(i+1):
                prev_flow=quantity[(i,j)]-1
                if prev_flow<=0:
                    continue
                flag=True
                quantity[(i,j)]=1
                quantity[(i+1,j)]+=prev_flow/2
                quantity[(i+1,j+1)]+=prev_flow/2
            if not flag: break
        return quantity[(r,c)]
// Champagne Tower
// Leetcode: https://leetcode.com/problems/champagne-tower/

class Solution {
    public double champagneTower(int poured, int query_row, int query_glass) {
        if (poured == 0) return 0;
        double[] memo = new double[101];
        memo[0] = poured;
        for (int i=0; i<100; i++) {
            for (int j=i; j>=0; j--) {
                if (memo[j] > 1) {
                    if (i == query_row && j == query_glass) return 1;
                    double val = (memo[j] - 1) / 2;
                    memo[j+1] += val;
                    memo[j] = val;
                } else {
                    if (i == query_row && j == query_glass) return memo[query_glass];
                    memo[j+1] += 0;
                    memo[j] = 0;
                }
            }
        }
        return memo[query_glass];
    }
}
class Solution {
public:
    double champagneTower(int poured, int query_row, int query_glass) {
        vector<double> currRow(1, poured);
		
        for(int i=0; i<=query_row; i++){ //we need to make the dp matrix only till query row. No need to do after that
            vector<double> nextRow(i+2, 0); //If we are at row 0, row 1 will have 2 glasses. So next row will have currRow number + 2 number of glasses.
            for(int j=0; j<=i; j++){ //each row will have currRow number + 1 number of glasses.
                if(currRow[j]>=1){ //if the champagne from the current glass is being overflowed.
                    nextRow[j] += (currRow[j]-1)/2.0; //fill the left glass with the overflowing champagne
                    nextRow[j+1] += (currRow[j]-1)/2.0; //fill the right glass with the overflowing champagne
                    currRow[j] = 1; //current glass will store only 1 cup of champagne
                }
            }
            if(i!=query_row) currRow = nextRow; //change the currRow for the next iteration. But if we have already reached the query_row, then the next iteration will not even take place, so the currRow is the query_row itself. So don't change as we need the currRow only.
        }
        return currRow[query_glass];
    }
};
var champagneTower = function(poured, query_row, query_glass) {
    
    let water = [poured];
    let hasOverflow = true;
    let row = 0;

    while(true){
        
        if (! hasOverflow) return 0   // We haven't reached query_row yet, and water ran out
        hasOverflow = false;
        
        let rowGlass = Array(water.length).fill(0);    // List of glasses at current row
        for (let i = 0; i < rowGlass.length; i++){
            let input = water[i];
            if (input <= 1){
                rowGlass[i] = input;
                water[i] = 0
            }else{
                rowGlass[i] = 1;
                water[i]--
            }
            if (row == query_row && i == query_glass){  // Return if we reach goal before water run out
                return rowGlass[i]
            }
        }
        // console.log('row,rowGlass',row,rowGlass);  // to debug
        
        let nextWater = Array(water.length + 1).fill(0);  // water poured towards next row, 
        for (let i = 0; i < rowGlass.length; i++){
            let overflow = water[i];
            
            if (overflow > 0) hasOverflow = true;
            
            nextWater[i] += overflow / 2;
            nextWater[i+1] += overflow / 2;
        }
        water = nextWater;
        row ++;
    }
};
Champagne Tower