Leetcode 799: Champagne Tower

grid47
grid47
Exploring patterns and algorithms
Aug 19, 2024 6 min read

A tower of champagne glasses where the champagne is poured, each glass softly glowing as it fills.
Solution to LeetCode 799: Champagne Tower Problem

You are given a champagne tower in the shape of a pyramid. The topmost glass is filled with a specified amount of champagne. When a glass is full, the excess champagne spills equally into the two glasses directly beneath it. Given the total amount of champagne poured, determine how full a particular glass at row query_row and glass query_glass will be after the champagne has spilled.
Problem
Approach
Steps
Complexity
Input: The input consists of three values: `poured`, the total amount of champagne poured into the topmost glass, and `query_row` and `query_glass`, which specify the row and position of the glass to query.
Example: Input: poured = 3, query_row = 2, query_glass = 1
Constraints:
• 0 <= poured <= 10^9
• 0 <= query_glass <= query_row < 100
Output: Return the amount of champagne in the queried glass, ensuring the result is rounded to five decimal places.
Example: Output: 0.50000
Constraints:
• The returned result must be accurate to five decimal places.
Goal: The goal is to simulate the pouring and spilling process and determine how full a specific glass is.
Steps:
• Initialize an array to represent the glasses, starting with the topmost glass filled with the specified amount of champagne.
• For each row, calculate the champagne that overflows from each glass, and distribute it equally between the two glasses directly beneath it.
• Repeat the process until the row of interest is reached, ensuring each glass's content is capped at 1 unit of champagne.
Goal: Ensure that the values of poured, query_row, and query_glass are within the defined limits.
Steps:
• The number of glasses is determined by the row number, with row i containing i+1 glasses.
• Champagne cannot overflow beyond the last row, so any excess champagne falls on the floor.
Assumptions:
• The number of glasses in each row is incremental, with the ith row containing i+1 glasses.
• Champagne only spills to the glasses directly beneath, left and right.
Input: Input: poured = 3, query_row = 2, query_glass = 1
Explanation: After pouring 3 cups of champagne, the first row has 1 cup, the second row has 1.5 cups (with each glass receiving 0.5 cups), and the third row has the second glass with 0.5 cups (because it receives a portion of the excess from the second row).

Input: Input: poured = 6, query_row = 2, query_glass = 0
Explanation: Pouring 6 cups results in the first row having 1 cup, the second row having 2 cups (with each glass full), and the third row has the two outer glasses each filled to 0.5 cups.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus