【LeetCode】799. Champagne Tower 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/champagne-tower/description/
题目描述
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.
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.)
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.)
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:
- poured will be in the range of [0, 10 ^ 9].
- query_glass and query_row will be in the range of [0, 99].
题目大意
往香槟塔最上面一层导入一定体积的香槟酒,求香槟塔每个位置杯子里的香槟体积。
解题方法
动态规划
这题使用类似动态规划的解法,需要解决的问题是从上一层的酒杯递推求出该层每个位置的酒的体积。
可以做以下思考:首先我们计算每个酒杯如果在没有往下分流的情况下,它会有多少体积的酒。然后分析这一层酒杯的酒如果满了,那么会流到哪里去?显然会均匀的流向下一层的两个相邻的杯子里去。
所以我们使用100 * 100的矩阵模拟每层的杯子,第一层的第一个杯子初始体积是倒入的酒的体积poured,然后向下递推,递推的方式下一层对应序号和下一层对应序号+1的两个杯子均分当前杯子超过1的部分。注意使用的是+=号,也就是下一层杯子的酒的体积是来自当前层流到里面酒体积的累加。
最后返回的时候需要再次判断,我们dp保存的是流经的液体的体积,不是真实的体积,所以和1取个最小值。
这题关键词: 二维数组,保存流经杯子的体积,只流到下一层相邻的两个杯子里,杯子液体体积累加。
本来使用的i遍历到N - 1即最后一层才结束,其实直接递推到题目要求的query_row就行了,后面的那些杯子不用管。
时间复杂度是O(N^2),空间复杂度是O(100*100).
class Solution(object):
def champagneTower(self, poured, query_row, query_glass):
"""
:type poured: int
:type query_row: int
:type query_glass: int
:rtype: float
"""
N = 100
dp = [[0] * N for _ in range(N)]
dp[0][0] = poured
for i in range(query_row):
for j in range(i + 1):
if dp[i][j] > 1:
dp[i + 1][j ] += (dp[i][j] - 1) / 2.0
dp[i + 1][j + 1] += (dp[i][j] - 1) / 2.0
return min(1, dp[query_row][query_glass])
参考资料
https://www.youtube.com/watch?v=OqXzKsEWM44
日期
2018 年 10 月 27 日 —— 10月份最后一个周末
【LeetCode】799. Champagne Tower 解题报告(Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- nrf51822 RAM不足分析
之前了解过STM32 的内存分配问题,对于蓝牙芯片51822的内存分配问题把项目中,遇到了.bss和.data部分超了的问题,这其实就是声明的变量和stask 及 heap的大小总和超出了单片机的RA ...
- bluetooth sig bluetooth asia-深圳之行
18年5月30日深圳参见蓝牙展会 主要了解下面 使用蓝牙和区块链构建室内导航定位系统和去中心化的MESH网络 -- 核心是通过iBeacon 来广播数据,典型用例是手机对手机的使用蓝牙进行交互,业界称 ...
- C#集合Dictionary中按值的排序
C#集合Dictionary中按值的降序排列 static void Main(string[] args) { Dictionary<string, int> d ...
- 【Redis集群原理专题】分析一下相关的Redis集群模式下的脑裂问题!
技术格言 世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程. 什么是脑裂 字面含义 首先,脑裂从字面上理解就是脑袋裂开了,就是思想分家了,就是有了两个山头,就是有了 ...
- C#表格,表格信息、GridView使用。
page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="test1.a ...
- 源码分析-Consumer
消息消费概述 消息消费以组的模式开展,一个消费组内可以包含多个消费者,每一个消费者组可订阅多个主题,消费组之间有集群模式和广播模式两种消费模式. 集群模式,主题下的同一条消息只允许被其中一个消费者消费 ...
- Shell学习(五)—— awk命令详解
一.awk简介 awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报 ...
- android 防止R被混淆,R类反射混淆,找不到资源ID
在Proguard.cfg中添加 -keep class **.R$* { *; }
- Output of C++ Program | Set 10
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 #include<st ...
- Spring组合注解与元注解
目录 注解说明 源代码 使用范例 注解说明 元注解:可以注解到别的注解上的注解,所以元注解首先基于条件@Target({ElementType.TYPE}) ,目标使用在类文件上 . 组合注解:连个元 ...