作者: 负雪明烛
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:

  1. 0 <= K <= N <= 10000
  2. 1 <= W <= 10000
  3. 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的时候,才会继续取数。那么状态转移方程可以写成:

  1. i <= K时,dp[i] = (前W个dp的和)/ W;(爬楼梯得到总楼梯数为i的概率)
  2. K < i < K + W时,那么在这次的前一次的点数范围是[i - W, K - 1]。我们的dp数组表示的是得到点i的概率,所以dp[i]=(dp[K-1]+dp[K-2]+…+dp[i-W])/W.(可以从前一次的基础的上选[1,W]个数字中的一个)
  3. 当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 日 —— 小光棍节

最新文章

  1. jelq
  2. PHP curl传 json字符串
  3. BZOJ 1856 字符串(组合)
  4. Mysql数据库插入的中文字段值显示问号的问题解决
  5. iOS 数据类型
  6. NC的开发模型
  7. async/await,了解一下?
  8. Vue重修02
  9. gulp + gulp-better-rollup + rollup 构建 ES6 开发环境
  10. JS日期选择器
  11. 你(可能)不知道的web api
  12. January 11th, 2018 Week 02nd Thursday
  13. android安全检测工具,梆梆安全 - 防止反编译|APP安全加固|应用加固|盗版监测
  14. memory prefix inter,intra,intro,iso out 5
  15. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem D. Distance 迪杰斯特拉
  16. AVAudioPlayer播放音频文件时没声音
  17. 一:Web
  18. Oracle ERP Profile
  19. Codeforces 221 B. Little Elephant and Numbers
  20. JavaSE 手写 Web 服务器(二)

热门文章

  1. MicrosoftPowerBI—2019-nCov 新型冠状病毒肺炎多功能动态看板
  2. 创建一个vue实例
  3. 标准非STL容器 : bitset
  4. 使用SpringBoot实现登录注册的几个问题
  5. 云原生PaaS平台通过插件整合SkyWalking,实现APM即插即用
  6. tensorboard No dashboards are active for the current data set.
  7. LeetCode替换空格
  8. Spark的shuffle和MapReduce的shuffle对比
  9. css相关,position定位详解
  10. Linux学习 - 变量测试与内容替换