Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 994 Bytes

799. 香槟塔.md

File metadata and controls

30 lines (23 loc) · 994 Bytes

799. 香槟塔

题目传送门

点击这里

解题思路

这道题的题意还是比较好理解的,和我们现实中的香槟塔一样,上面的杯子如哦个装满溢出就会有水留到下方的支撑的两个杯子里,我们只需要模拟遍历这个过程即可。题目说明了,杯子的位置都从第0行第0个开始计算。

代码

func champagneTower(poured int, query_row int, query_glass int) float64 {
    // row使用滚动数组的思想,更新每一层香槟的状态,第一层第一个存放倒入的所有的量,然后向下计算
    row := []float64{float64(poured)}
    for i := 1;i <= query_row;i ++ {
        nextRow := make([]float64, i + 1)
        for j, v := range row {
            if v > 1 {
                nextRow[j] += (v-1)/2
                nextRow[j+1] += (v-1)/2
            }
        }
        row = nextRow
    }
    return math.Min(1, row[query_glass])
}