Champagne Tower - Problem
Champagne Tower Problem
Imagine you're at an elegant celebration where champagne glasses are arranged in a beautiful pyramid formation. The setup is breathtaking: 1 glass at the top, 2 glasses in the second row, 3 glasses in the third row, and so on, creating a perfect triangular tower up to 100 rows.
๐ฅ The Physics: When champagne is poured into the topmost glass, it follows the laws of gravity and overflow. Once a glass becomes completely full (holds exactly 1 cup), any additional champagne splits equally between the two glasses directly below it - one to the left diagonal and one to the right diagonal.
๐ฏ Your Mission: Given that
Input: Three integers -
Output: A decimal representing how full the specified glass is (clamped between 0.0 and 1.0)
Imagine you're at an elegant celebration where champagne glasses are arranged in a beautiful pyramid formation. The setup is breathtaking: 1 glass at the top, 2 glasses in the second row, 3 glasses in the third row, and so on, creating a perfect triangular tower up to 100 rows.
๐ฅ The Physics: When champagne is poured into the topmost glass, it follows the laws of gravity and overflow. Once a glass becomes completely full (holds exactly 1 cup), any additional champagne splits equally between the two glasses directly below it - one to the left diagonal and one to the right diagonal.
๐ฏ Your Mission: Given that
poured cups of champagne are poured into the top glass, determine how full the glass at position (queryRow, queryGlass) is. The result should be a decimal between 0.0 (empty) and 1.0 (completely full).Input: Three integers -
poured (amount of champagne), queryRow and queryGlass (both 0-indexed)Output: A decimal representing how full the specified glass is (clamped between 0.0 and 1.0)
Input & Output
example_1.py โ Basic Overflow
$
Input:
poured = 1, query_row = 1, query_glass = 1
โบ
Output:
0.00000
๐ก Note:
With only 1 cup poured, the top glass becomes full (1.0) but has no overflow. The glasses in row 1 remain empty.
example_2.py โ Equal Distribution
$
Input:
poured = 2, query_row = 1, query_glass = 1
โบ
Output:
0.50000
๐ก Note:
The top glass holds 1.0, and the remaining 1.0 cup splits equally between the two glasses in row 1. Each gets 0.5.
example_3.py โ Multiple Level Overflow
$
Input:
poured = 100000009, query_row = 33, query_glass = 17
โบ
Output:
1.00000
๐ก Note:
With an enormous amount of champagne, virtually all glasses in the pyramid become completely full (1.0).
Constraints
- 0 โค poured โค 109
- 0 โค query_row, query_glass โค 99
- query_glass โค query_row
- All inputs are integers except poured can be float
- Answer within 10-5 of actual answer is accepted
Visualization
Tap to expand
Understanding the Visualization
1
๐พ Initial Pour
All champagne starts in the topmost glass
2
๐ง Overflow Check
If a glass holds more than 1.0, it overflows
3
โ๏ธ Equal Split
Excess champagne splits equally between two glasses below
4
๐ Level Processing
Process entire level before moving to next
5
๐ฏ Target Reached
Return the amount in our target glass
Key Takeaway
๐ฏ Key Insight: Model the natural physics - champagne flows from top to bottom, level by level. Each glass holds at most 1.0 units, and excess splits equally between its two children below. Process complete levels sequentially for optimal efficiency.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code