Sunday, September 24th
Champagne
Given a pyramid of champagne glasses and an amount poured into the top glass, determine how much champagne will be in a given glass in the pyramid
This problem can be solved in a straightforward manner, where, for a given pour we will determine the amount of champagne in every glass in the pyramid , then return the results for the given glasses. There are only 100 rows total so it is feasible.
To begin, we initialize a 2D-array which will represent the pyramid. We also add the total amount poured to the top glass
Then we iterate from the first row down, and by the left glass to the right glass
If for a given glass, the value of the glass is more than 1, then we calculate the amount of champagne spilled over to the 2 glasses below it. Then we set the amount in the original glass to 1
Now that we have calculated the champagne in every glass, we return the answer for the given row and glass
A little tricky problem since this straightforward approach isn't used too often. Calculating all values and the returning the one doesn't seem intuitive to start. Also, the structure of the problem kind of seems like Pascal's triangle which would call for dynamic programming.
Comments
Post a Comment