[Swift]LeetCode799. 香檳塔 | Champagne Tower

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-sfijxzzz-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.  Each glass holds one cup (250ml) of champagne.git

Then, some champagne is poured in the first glass at the top.  When the top most glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it.  When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on.  (A glass at the bottom row has it's excess champagne fall on the floor.)github

For example, after one cup of champagne is poured, the top most glass is full.  After two cups of champagne are poured, the two glasses on the second row are half full.  After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now.  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 j-th glass in the i-th row is (both i and j are 0 indexed.)ide

Example 1:
Input: poured = 1, query_glass = 1, query_row = 1
Output: 0.0
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_glass = 1, query_row = 1
Output: 0.5
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.

Note:ui

  • poured will be in the range of [0, 10 ^ 9].
  • query_glass and query_row will be in the range of [0, 99].

咱們把玻璃杯擺成金字塔的形狀,其中第一層有1個玻璃杯,第二層有2個,依次類推到第100層,每一個玻璃杯(250ml)將盛有香檳。spa

從頂層的第一個玻璃杯開始傾倒一些香檳,當頂層的杯子滿了,任何溢出的香檳都會馬上等流量的流向左右兩側的玻璃杯。當左右兩邊的杯子也滿了,就會等流量的流向它們左右兩邊的杯子,依次類推。(當最底層的玻璃杯滿了,香檳會流到地板上)code

例如,在傾倒一杯香檳後,最頂層的玻璃杯滿了。傾倒了兩杯香檳後,第二層的兩個玻璃杯各自盛放一半的香檳。在倒三杯香檳後,第二層的香檳滿了 - 此時總共有三個滿的玻璃杯。在倒第四杯後,第三層中間的玻璃杯盛放了一半的香檳,他兩邊的玻璃杯各自盛放了四分之一的香檳,以下圖所示。htm

如今當傾倒了非負整數杯香檳後,返回第 i 行 j 個玻璃杯所盛放的香檳佔玻璃杯容積的比例(i 和 j都從0開始)。blog

 

示例 1:
輸入: poured(傾倒香檳總杯數) = 1, query_glass(杯子的位置數) = 1, query_row(行數) = 1
輸出: 0.0
解釋: 咱們在頂層(下標是(0,0))倒了一杯香檳後,沒有溢出,所以全部在頂層如下的玻璃杯都是空的。

示例 2:
輸入: poured(傾倒香檳總杯數) = 2, query_glass(杯子的位置數) = 1, query_row(行數) = 1
輸出: 0.5
解釋: 咱們在頂層(下標是(0,0)倒了兩杯香檳後,有一杯量的香檳將從頂層溢出,位於(1,0)的玻璃杯和(1,1)的玻璃杯平分了這一杯香檳,因此每一個玻璃杯有一半的香檳。

注意:

  • poured 的範圍[0, 10 ^ 9]
  • query_glass 和query_row 的範圍 [0, 99]

Runtime: 40 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func champagneTower(_ poured: Int, _ query_row: Int, _ query_glass: Int) -> Double {
 3         var dp:[[Double]] = [[Double]](repeating:[Double](repeating:0,count:101),count:101)
 4         dp[0][0] = Double(poured)
 5         for i in 0...query_row
 6         {
 7             for j in 0...i
 8             {
 9                 if dp[i][j] >= 1
10                 {
11                     dp[i + 1][j] += (dp[i][j] - 1) / 2.0
12                     dp[i + 1][j + 1] += (dp[i][j] - 1) / 2.0
13                 }
14             }
15         }
16         return min(1.0, dp[query_row][query_glass])
17     }
18 }
相關文章
相關標籤/搜索