【LeetCode】837. New 21 Game 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/new-21-game/description/
题目描述
Alice plays the following game, loosely based on the card game “21”.
Alice starts with 0
points, and draws numbers while she has less than K
points. During each draw, she gains an integer number of points randomly from the range [1, W]
, where W
is an integer. Each draw is independent and the outcomes have equal probabilities.
Alice stops drawing numbers when she gets K
or more points. What is the probability that she has N
or less points?
Example 1:
Input: N = 10, K = 1, W = 10
Output: 1.00000
Explanation: Alice gets a single card, then stops.
Example 2:
Input: N = 6, K = 1, W = 10
Output: 0.60000
Explanation: Alice gets a single card, then stops.
In 6 out of W = 10 possibilities, she is at or below N = 6 points.
Example 3:
Input: N = 21, K = 17, W = 10
Output: 0.73278
Note:
- 0 <= K <= N <= 10000
- 1 <= W <= 10000
- Answers will be accepted as correct if they are within 10^-5 of the correct answer.
The judging time limit has been reduced for this question.
题目大意
刚开始的时候,有0分,她会已知在[1,W]中随机选数字,直到有K分或者K分以上停止。问她能够正好得到N分或者更少分的概率。
解题方法
动态规划
类似爬楼梯的问题,每次可以跨[1,W]个楼梯,当一共爬了K个和以上的台阶时停止,问这个时候总台阶数<=N的概率。
使用动态规划,dp[i]表示得到点数i的概率,只有当现在的总点数少于K的时候,才会继续取数。那么状态转移方程可以写成:
- 当
i <= K
时,dp[i] = (前W个dp的和)/ W
;(爬楼梯得到总楼梯数为i的概率) - 当
K < i < K + W
时,那么在这次的前一次的点数范围是[i - W, K - 1]
。我们的dp数组表示的是得到点i的概率,所以dp[i]=(dp[K-1]+dp[K-2]+…+dp[i-W])/W
.(可以从前一次的基础的上选[1,W]个数字中的一个) - 当i>=K+W时,这种情况下无论如何不都应该存在的,所以dp[i]=0.
时间复杂度是O(N),空间复杂度是O(N).
class Solution(object):
def new21Game(self, N, K, W):
"""
:type N: int
:type K: int
:type W: int
:rtype: float
"""
if K == 0: return 1
dp = [1.0] + [0] * N
tSum = 1.0
for i in range(1, N + 1):
dp[i] = tSum / W
if i < K:
tSum += dp[i]
if 0 <= i - W < K:
tSum -= dp[i - W]
return sum(dp[K:])
相似题目
参考资料
https://blog.csdn.net/qq_20141867/article/details/81261711
日期
2018 年 11 月 1 日 —— 小光棍节
【LeetCode】837. New 21 Game 解题报告(Python)的更多相关文章
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
随机推荐
- 利用vcftools比较两个vcf文件
因为最近有一项工作是比较填充准确性的,中间有用到vcftools比较两个vcf文件. 使用命令也很简单: 1 vcftools --vcf file1.snp.vcf --diff file2.snp ...
- git放弃修改,强制覆盖本地代码
1.git fetch --all //从远程拉取最新的代码 不merge 2.git reset --hard origin/develop //使用指定分支的代码(此处develop)强制覆盖 ...
- 7个连环问揭开java多线程背后的弯弯绕
摘要:很多java入门新人一想到java多线程, 就会觉得很晕很绕,什么可见不可见的,也不了解为什么sync怎么就锁住了代码. 本文分享自华为云社区<java多线程背后的弯弯绕绕到底是什么? 7 ...
- college-ruled notebook
TBBT.s3.e10: Sheldon: Where's your notebook?Penny: Um, I don't have one.Sheldon: How are you going t ...
- Java程序逻辑控制
程序逻辑控制 1.Java程序结构与逻辑控制 在Java中程序有三种结构:顺序结构.分支结构.循环结构 1.1 Java分支结构 分支结构:进行逻辑判断,当满足某些条件时才会执行某些语句. 1.if语 ...
- EBS 抓trace 文件
如果要对FORM的操作做TRACE操作,可以使用 帮助->诊断->跟踪 中启用跟踪功能来实现. 但是如果要实现对并发请求的trace,需要在 系统管理员->并发->方案-> ...
- rust方法集
随机数.数字对比.控制台输入 use std::io; use std::cmp::Ordering; use rand::Rng; fn main() { println!("please ...
- Mysql多字段模糊查询
MySQL同一字段多值模糊查询 一. 同一字段多值模糊查询,使用多个or进行链接,效率不高,但没有更好的解决方案.(有看到CHARINDEX 关键字,可查询结果并不是模糊,举个栗子 例如SELECT ...
- Dubbo服务暴露延迟
Dubbo 2.6.5 版本以后,如果我们的服务启动过程需要warmup事件,就可以使用delay进行服务延迟暴露.只需在服务提供者的<dubbo:service/>标签中添加delay属 ...
- my40_MySQL锁概述之意向锁
本文在锁概述的基础上,通常实验举例,详细地介绍了意向锁的原理. 锁范围 全局锁(global lock)表锁(table lock)行锁 (row lock) ROW LOCK的粒度LOCK_REC ...