Champagne Tower

Medium Topics Company Tags

A tower has 100 rows of glasses. Row r contains glasses numbered 0 through r, and each glass holds one cup. Pour poured cups into glass (0,0). A glass keeps up to one cup and divides any excess equally between glasses (r+1,c) and (r+1,c+1) directly below it. Excess below the last row is discarded. Return the amount in glass (query_row,query_glass) after the liquid settles, as a number from 0 to 1. Rows and columns are zero-indexed.

Hatching shows how full each glass is; the dashed box marks the queried glass.

Example 1:

Champagne Tower, example 1: illustration of the example

Input: poured = 4
query_row = 2
query_glass = 1

Output: 0.5

Explanation: The top glass keeps 1 cup and sends 1.5 cups to each glass in row 1. Each of those glasses sends 0.25 cups to the middle glass in row 2, giving it 0.5 cups.

Example 2:

Champagne Tower, example 2: illustration of the example

Input: poured = 4
query_row = 2
query_glass = 0

Output: 0.25

Explanation: Each glass in row 1 receives 1.5 cups. The leftmost glass in row 2 receives half of the left parent's 0.5-cup excess, so it contains 0.25 cups.

Example 3:

Champagne Tower, example 3: illustration of the example

Input: poured = 0
query_row = 0
query_glass = 0

Output: 0.0

Explanation: No liquid is poured, so the top glass stays empty.

Constraints:

  • 0 <= poured <= 1000000000
  • 0 <= query_glass <= query_row < 100


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.