原题链接在这里:https://leetcode.com/problems/new-21-game/

题目:

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.
  4. The judging time limit has been reduced for this question.

题解:

When the draws sum up to K, it stops, calculate the possibility K<=sum<=N.

Think about one step earlier, sum = K-1, game is not ended and draw largest card W. K-1+W is the maximum sum could get when game is ended. If it is <= N, then for sure the possiblity when games end ans sum <= N is 1.

Because the maximum is still <= 1.

Otherwise calculate the possibility sum between K and N.

Let dp[i] denotes the possibility of that when game ends sum up to i.

i is a number could be got equally from i - m and draws value m card.

Then dp[i] should be sum of dp[i-W] + dp[i-W+1] + ... + dp[i-1], devided by W.

We only need to care about previous W value sum, accumlate winSum, reduce the possibility out of range.

Time Complexity: O(N).

Space: O(N).

AC Java:

 class Solution {
public double new21Game(int N, int K, int W) {
if(K == 0 || K-1+W <= N){
return 1;
} if(K > N){
return 0;
} double [] dp = new double[N+1];
dp[0] = 1.0;
double winSum = 1; double res = 0.0;
for(int i = 1; i<=N; i++){
dp[i] = winSum/W; if(i<K){
winSum += dp[i];
}else{
res += dp[i];
} if(i >= W){
winSum -= dp[i-W];
}
} return res;
}
}

类似Climbing Stairs.

LeetCode 837. New 21 Game的更多相关文章

  1. Java实现 LeetCode 837 新21点(DP)

    837. 新21点 爱丽丝参与一个大致基于纸牌游戏 "21点" 规则的游戏,描述如下: 爱丽丝以 0 分开始,并在她的得分少于 K 分时抽取数字. 抽取时,她从 [1, W] 的范 ...

  2. 【LeetCode】837. New 21 Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  3. 【leetcode】837. New 21 Game

    题目如下: 解题思路:这个题目有点像爬楼梯问题,只不过楼梯问题要求的计算多少种爬的方式,但是本题是计算概率.因为点数超过或者等于K后就不允许再增加新的点数了,因此我们可以确定最终Alice拥有的点数的 ...

  4. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  5. 【leetcode❤python】21. Merge Two Sorted Lists

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  6. Leetcode题解(21)

    62. Unique Paths 题目 分析: 机器人一共要走m+n-2步,现在举个例子类比,有一个m+n-2位的二进制数,现在要在其中的m位填0,其余各位填1,一共有C(m+n-2,m-1)种可能, ...

  7. [LeetCode&Python] Problem 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  8. Leetcode题库——21.合并两个有序链表

    @author: ZZQ @software: PyCharm @file: mergeTwoLists.py @time: 2018/9/16 20:49 要求:将两个有序链表合并为一个新的有序链表 ...

  9. LeetCode记录之21——Merge Two Sorted Lists

    算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...

随机推荐

  1. php中让数组顺序随机化,打乱顺序等

    php中有很多排序的函数,sort,rsort,ksort,krsort,asort,arsort,natcasesort,这些函数用来对数组的键或值进行这样,或那样的排序. 可以终究有时候还需要一些 ...

  2. C/C++語言 - 日常算法 - 蛇形填數

    C/C++語言 - 日常算法 - 蛇形填數 日期 : 2019-06-11 問題描述: 在n×n方阵里填入1,2,…,n×n,要求填成蛇形. 例如,n=4时方阵为: 10   11  12  1 9 ...

  3. 深度学习-生成对抗网络GAN笔记

    生成对抗网络(GAN)由2个重要的部分构成: 生成器G(Generator):通过机器生成数据(大部分情况下是图像),目的是“骗过”判别器 判别器D(Discriminator):判断这张图像是真实的 ...

  4. 最新版Prometheus+Grafana+node-exporter炫酷界面

    一.概述 理论知识就不多介绍了,参考链接: https://www.cnblogs.com/xiao987334176/p/9930517.html 本文使用2台服务器,来搭建. 环境 操作系统 do ...

  5. timeout超时时长优化和hystrix dashboard可视化分布式系统

    在生产环境中部署一个短路器,一开始需要将一些关键配置设置的大一些,比如timeout超时时长,线程池大小,或信号量容量 然后逐渐优化这些配置,直到在一个生产系统中运作良好 (1)一开始先不要设置tim ...

  6. jQuery.Form.js使用方法

    一.jQuery.Form.js 插件的作用是实现Ajax提交表单. 方法: 1.formSerilize() 用于序列化表单中的数据,并将其自动整理成适合AJAX异步请求的URL地址格式. 2.cl ...

  7. crunch制作字典

    安装 安装crunch sudo apt-get install crunch 语法 crunch <min> max<max> <characterset> -t ...

  8. MySQL数据库汇总

    -- mysql的最大连接数:默认为 100   -- mysql的增删改查   -- mysql统计各个字段(case when 用法 注:也可以使用其他的)   select (case when ...

  9. python day 20: 线程池与协程,多进程TCP服务器

    目录 python day 20: 线程池与协程 2. 线程 3. 进程 4. 协程:gevent模块,又叫微线程 5. 扩展 6. 自定义线程池 7. 实现多进程TCP服务器 8. 实现多线程TCP ...

  10. Windows VNC远程连接用法

    VNC (Virtual Network Console)是虚拟网络控制台 被控端 被控端需要打开服务,等待主控端连接 服务端已经启动成功,右下角有小图标 主控端 打开主控端,连接被控端 输入被控端i ...