【leetcode】837. New 21 Game
题目如下:

解题思路:这个题目有点像爬楼梯问题,只不过楼梯问题要求的计算多少种爬的方式,但是本题是计算概率。因为点数超过或者等于K后就不允许再增加新的点数了,因此我们可以确定最终Alice拥有的点数的区间是[K,K-1+W],下限等于K很好理解,Alice最后一次抽取点数前可能拥有的点数最大值是K-1,最后一次抽取的点数最大值是W,因此上限就是K-1+W。和爬楼梯类似,恰好获得点数n的概率dp[n] = sum(dp[n-w]/w + dp[n-w+1]/w + .... dp[n-1]/w)。因为获取任意一个点数的概率都是1/W,所以上面的公式中每个dp都要除以W。但是题目约定了一个K值,在n > k + 1的情况下,dp[n]是无法通过dp[n-1]得到,需要修正公式: dp[n] = sum(dp[n-w]/w + dp[n-w+1]/w + .... dp[K-1]/w)。最后,点数小于或者等于N的概率就是 sum(dp[K:N + 1])。
代码如下:
class Solution(object):
def new21Game(self, N, K, W):
"""
:type N: int
:type K: int
:type W: int
:rtype: float
"""
low = K
high = K - 1 + W
if N < low or K == 0 or N > high:
return 1.0
dp = [0 for x in xrange(high + 1)]
dp[0] = 0.0
for i in xrange(1, min(high, W) + 1):
dp[i] = float(1) / float(W)
# print dp
pro = 0.0
for i in xrange(2, W + 1):
if i > K:
maxv = K - 1
minv = max(i - W, 1)
else:
maxv = i - 1
minv = max(i - W, 1)
if pro == 0.0:
for j in xrange(minv, maxv + 1):
pro += dp[j]/W
else:
if i > K:
pro -= dp[minv-1]/W
else:
pro += dp[maxv]/W
pro -= dp[minv-1]/W
dp[i] += pro
lastWCount = sum(dp[:min(K, W + 1)])
#print lastWCount
for i in xrange(W + 1, len(dp)):
dp[i] = lastWCount / W
if i < K:
lastWCount += dp[i]
lastWCount -= dp[i - W]
#print '1:',i,dp[i]
#print 'total:',sum(dp[low:])
#print '1',dp[K-5:K+5]
#print dp
return sum(dp[low:N + 1])/sum(dp[low:])
【leetcode】837. New 21 Game的更多相关文章
- 【LeetCode】837. New 21 Game 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
随机推荐
- allure2生成html报告
前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1. ...
- Delphi驱动方式WINIO模拟按键 可用
http://www.delphitop.com/html/yingjian/152.html Delphi驱动方式WINIO模拟按键 作者:admin 来源:未知 日期:2010/2/1 1:14: ...
- AndroChef Java Decompile
With AndroChef Java Decompiler you can decompile apk., dex, jar and java class-files. It's simple an ...
- http层负载均衡之 haproxy实践篇
方案 上篇文章讲到了负载均衡的相关理论知识,这篇文章我打算讲讲实践方法以及实践中遇到的问题 方案:haproxy http层负载均衡 安装一个haproxy服务,两个web服务 haproxy:192 ...
- spring(二) AOP注入
AOP概念 l AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码 l 经典应用:事务管理.性能监视.安全检查.缓存 .日志等 l Spring AOP使用纯Java实现,不需要专门的编译 ...
- Java ——重写、多态、抽象类
本节重点思维导图 重写 子类覆盖父类同名的方法 final关键字:不可变的 public static final PAGE_SIZE = 18; final修饰的类不能做为父类被子类继承. 多态 多 ...
- Linux基本服务
一.Samba服务 1.下载samba yum install samba -y 2.配置samba文件 vim /etc/samba/smb.conf [ken]path = /test #等 ...
- 选择排序--python
def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1, len(arr)): if arr[i] & ...
- 区间gcd
http://codeforces.com/problemset/problem/914/D 题意:给你n个数,两种操作:1.询问区间[l,r]在至多一次修改一个数的条件下区间gcd是否等于x. 2. ...
- 【WPS/Visio】WPS word无法复制或编辑Visio对象
前言 Win10,WPS2019,Visio2016. 好像是有一次设置 .vsdx 的默认打开方式为Visio,之后每次在WPS里复制Visio对象,或双击编辑WPS word中以前的Visio对象 ...