【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 ...
随机推荐
- 安装mysql出现Couldn't find MySQL server (/usr/bin/mysqld_safe)
安装mysql出现Couldn't find MySQL server (/usr/bin/mysqld_safe)Starting MySQL ERROR! Couldn't find MySQL ...
- linux防火墙iptables简单介绍
--append -A chain Append to chain --delete -D chain Delete matching rule from chain ...
- 12.持久性后门----Ettercap之ARP中毒----RAR/ZIP & linux密码破解----kali上检测rootkits
持久性后门 生成PHP shell weevely generate 密码 /root/Desktop/404.php 靶机IP/404.php weevely http://192.168.1.10 ...
- jeecg项目将workbook 的Excel流添加到zip压缩包里导出
1.直接献出代码 Map<String,List<ConfidentialInformation>> typeMap = new HashMap<>(); try ...
- 关于android工具链
1 android sdk platform tools 同android platform交互的工具,包括adb.fastboot和systrace. 2 sdk build tools 用于bui ...
- urllib库认证,代理,cookie
认证,代理,cookie 1from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, buil ...
- PostgreSQL dblink使用过程
安装: 进入/root/postgresql-11.2/contrib/dblink make && make install 切换到postgres用户 [root@fce40690 ...
- servlet3.0文件上传与下载
描述:文件上传与下载是在JavaEE中常见的功能,实现文件上传与下载的方式有多种,其中文件上传的方式有: (1)commons-fileupload: (2)Servlet 3.0 实现文件上传 (3 ...
- 循环Gray码的生成(非递归)
#!/usr/bin/env python #coding:utf-8 import sys def gray_code(n): if n < 1: return [] n += 1 array ...
- Luogu P4040 [AHOI2014/JSOI2014]宅男计划
题目 显然存活天数与叫外卖次数的函数是凸函数. 所以三分买外卖的次数. 然后把食品按保质期升序排序. 并且单调栈搞一下,把又贵又保质期短的丢掉. 那么随着保质期的增加,食品的价格一定上涨. 所以我们从 ...