作者: 负雪明烛
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)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. 毕业设计之mysql+主从复制+keepalived

    环境介绍 mysql_VIP:192.168.111.123 mysql_M!:192.168.111.151 mysql_M2:192.168.111.152 安装mysql可以查看 两个数据库都需 ...

  2. LeetCode 第一题 两数之和

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...

  3. 零基础学习java------38---------spring中关于通知类型的补充,springmvc,springmvc入门程序,访问保护资源,参数的绑定(简单数据类型,POJO,包装类),返回数据类型,三大组件,注解

    一. 通知类型 spring aop通知(advice)分成五类: (1)前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常. (2)正常返回通知 ...

  4. Java——数组的定义与使用

    数组的定义与使用 1.数组的基本概念 (1)数组的动态初始化: 数组首先先开辟内存空间,而后再使用索引进行内容的设置,这种定义数组的方式称为动态初始化 数组是引用数据类型,存在有内存分配问题.在使用前 ...

  5. html块 布局

    可通过<div>和<span>将html元素组合起来. Html块元素 大多数html元素被定义为块级元素或内联元素. 块级元素在浏览器显示时,通常会以新行来开始(和结束).例 ...

  6. 3.3 GO字符串处理

    strings方法 index 判断子字符串或字符在父字符串中出现的位置(索引)Index 返回字符串 str 在字符串 s 中的索引( str 的第一个字符的索引),-1 表示字符串 s 不包含字符 ...

  7. 学习 27 门编程语言的长处,提升你的 Python 代码水平

    Python猫注:Python 语言诞生 30 年了,如今的发展势头可谓如火如荼,这很大程度上得益于其易学易用的优秀设计,而不可否认的是,Python 从其它语言中偷师了不少.本文作者是一名资深的核心 ...

  8. HashMap的putAll方法介绍说明

    jdk1.8 使用putAll时,新map中的值仅为旧map值所对应对象的引用,并不会产生新对象. 如下,使用for循环赋值! public void putAll(Map<? extends ...

  9. mobile app 与server通信的四种方式

    Have you ever wondered how the information gets into the application installed in your mobile device ...

  10. cmcc_simplerop

    这是一道系统调用+rop的题. 先来就检查一下保护. 32位程序,只开启了堆栈不可执行.ida看一下伪代码. 代码也很简洁,就是直接让你溢出.这里ida反汇编显示的v4具体ebp的距离是0x14,再加 ...