【leetcode】Champagne Tower
题目如下:

解题思路:本题如果用递归来做,思路会非常清晰。每个杯子得到的总的香槟的数量,减去自身杯子容量后,多余的部分均分成两部分,下层的两个杯子各得一半,但是这种解法在输入香槟较大的情况下会导致超时。更加合适的是用动态规划的方法,因为递推关系式很容易就能找到。对于任意一个杯子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的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- Linux centos 解决"不在 sudoers 文件中。此事将被报告"的问题
第一部分讲解如何从普通用户到root用户 网上看了很多,其实就是执行 su root ,然后输入当前用户密码,这个时候可能报错 这个问题不大,执行一下提示的语句: abrt-auto-reportin ...
- 数组Array用法
一 创建数组 // 指定长度(稀疏数组) const arr1 = Array(2); console.log(arr1); const arr2 = new Array(4); console.lo ...
- python每日一练:0015题
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示: { "1" : "上海", "2" : & ...
- Linux 自学shell
1.多个命令用";"分号分割 还可以使用alias 给命令取别名 alias foo='cd /usr ; ls; cd -'2.使用管道线"|" 一个命令的标 ...
- VS2013启动 外接程序VMDebugger未能加载或导致了异常
故障现象:打开Visual Studio 2010后弹出错误框,外接程序VMDebugger未能加载或导致了异常,是否希望移除该外接程序,错误号:80004005.系统版本:WIN10 64位专业版, ...
- SpringBoot整合mybatis碰到的问题
整合mybatis 1. 导包:在原有的web项目的基础上加上 <!--JDBC连接--> <dependency> <groupId>m ...
- 【7.24校内交流赛】T1&T2
T1: 一个脑洞很大的题,将输入的所有数异或起来输出就好了: (话说我为什么这么喜欢用异或啊) #include<bits/stdc++.h> using namespace std; i ...
- PC端微信防撤回功能分析
1.打开PC端微信的安装目录,有一个WeChatWin.dll文件,微信的所有功能基本上都在这个文件中了 2.OD打开,搜索字符串revokemsg(撤回消息,掌握一门外语是多么的重要啊!!!),在所 ...
- 题解 CF978C 【Letters】
此题评测机出了点问题,数据全部AC,却显示UKE 下面是数据全部AC,却显示UKE的代码 思路:b[i]减去每个宿舍的房间总数,如果b[i]小于了某个宿舍的房间总数则为答案. #include< ...
- IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)(转载)
创建parent项目 1.打开IDEA,注意这里不要勾选模板,用模板创建过maven项目的小伙伴都知道模板创建项目非常慢,所以这里不要选模板,需要的文件夹我们后面自己来创建就可以了.所以这个页面直接点 ...