题目如下:

解题思路:本题如果用递归来做,思路会非常清晰。每个杯子得到的总的香槟的数量,减去自身杯子容量后,多余的部分均分成两部分,下层的两个杯子各得一半,但是这种解法在输入香槟较大的情况下会导致超时。更加合适的是用动态规划的方法,因为递推关系式很容易就能找到。对于任意一个杯子dp[i][j]来说,它的输入来自于dp[i-1][j]和dp[i-1][j-1]溢出的部分的一半,所以表达式为 dp[i][j] =  max(0,(dp[i-1][j] - 1)/2) + max(0,(dp[i-1][j-1]-1)/2)。

代码如下:

class Solution(object):
def champagneTower(self, poured, query_row, query_glass):
"""
:type poured: int
:type query_row: int
:type query_glass: int
:rtype: float
"""
dp = []
level = 100
for x in range(level):
l = [0.0 for x in range(level)]
dp.append(l)
dp[0][0] = poured
for i in range(1,query_row+1):
for j in range(query_glass+1):
dp[i][j] += max(0,float(dp[i-1][j] - 1)/2)
if j-1 >= 0:
dp[i][j] += max(0,float(dp[i-1][j-1]-1)/2)
return min(1,dp[query_row][query_glass])

【leetcode】Champagne Tower的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. HttpClient设置忽略SSL,实现HTTPS访问, 解决Certificates does not conform to algorithm constraints

    话不多说,直接上代码. 测试API:   https://api.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f7 ...

  2. spring boot + mybatis 连接 oracle 出现 ORA-00923: 未找到要求的 FROM 关键字 错误

    1.原因 hikari 连接池配置错误,mysql和oracle的配置不一样 2.修改 spring: datasource: hikari: connection-test-query: selec ...

  3. HTML5——添加新元素 新元素 Canvas SVG MathML 黑客帝国特效

    为HTML添加新元素 添加新元素   +   该元素定义样式 <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  4. Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.2 Applications of Propositional Logic

    Translating English Sentences System Specifications Boolean Searches Logic Puzzles Logic Circuits

  5. Win10.Shift+鼠标右键_CMD(管理员)

    1.资料: 1.1.Windows10下设置Shift+右键增加cmd - wyx0712的博客 - CSDN博客.html(https://blog.csdn.net/wyx0712/article ...

  6. kafka学习(二)

    创建kafka生产者 要往kafka写入消息,首先要创建一个生产者对象,并设置一些熟悉.kafka生产者有3个必选的属性.   1.bootstrap.servers 该属性指定broker的地址清单 ...

  7. Pyinstaller-封装python

    1. 当程序中没有调用matplotlib模块 ① pip intall pyinstaller ② 在cmd环境下,pyinstaller -F  xxx.py 2.当程序中调用matplotlib ...

  8. CentOS7 NAT配置

    环境说明:Cloud1中的GE0/0/1.GE0/0/3.GE0/0/5接口,分别与Centos7中的eth1.eth2.eth3接口桥接到同一虚拟网卡,R1,R2,R3均配置一条静态默认路由指向Ce ...

  9. laravel框架之增刪改查

    <?php namespace App\Http\Controllers\admin; use Illuminate\Http\Request as request; use App\Http\ ...

  10. Python中函数传递参数有四种形式

    Python中函数传递参数有四种形式 fun1(a,b,c) fun2(a=1,b=2,c=3) fun3(*args) fun4(**kargs) 四种中最常见是前两种,基本上一般点的教程都会涉及, ...