原题链接在这里: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. CKEditor图片上传问题(默认安装情况下编辑器无法处理图片),通过Base64编码字符串解决

    准备做一个文章内容网站,网页编辑器采用CKEditor,第一次用,默认安装情况下,图片无法插入,提示没有定义上传适配器(adapter),错误码提示如下: 根据提示,在官网看到有两种途径:一使用CKE ...

  2. 【论文排版工具】——LaTeX的安装及使用(MiKTeX+TexStudio+Windows)

    版权声明:本文为CSDN博主「豆豆花」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/weixin_405 ...

  3. ZYNQ笔记(3):GPIO的使用(MIO、EMIO)——led灯

    一.GPIO原理 1.GPIO介绍 程序员通过软件代码可以独立和动态地对每个 GPIO 进行控制,使其作为输入.输出或中断. (1)通过一个加载指令,软件可以读取一个 GPIO 组内所有 GPIO 的 ...

  4. chrome Network 过滤和高级过滤

    转自:https://blog.csdn.net/tengdazhang770960436/article/details/90644523

  5. Python进阶----进程间数据隔离, join阻塞等待, 进程属性, 僵尸进程和孤儿进程, 守护进程

    Python进阶----进程间数据隔离, join阻塞等待, 进程属性, 僵尸进程和孤儿进程, 守护进程 一丶获取进程以及父进程的pid 含义:    进程在内存中开启多个,操作系统如何区分这些进程, ...

  6. 【开发笔记】- 在Windows环境下后台启动redis

    1. 进入 DOS窗口 2. 在进入Redis的安装目录 3. 输入:redis-server --service-install redis.windows.conf --loglevel verb ...

  7. Shallow copy and Deep copy

    Shallow copy and Deep copy 第一部分: 一.来自wikipidia的解释: Shallow copy One method of copying an object is t ...

  8. Java的基本知识之线程池篇

    1.基本概念 1.共享资源 多个线程对同一份资源进行访问(读写操作),该资源被称为共享资源.如何保证多个线程访问到的数据是一致的,则被称为数据同步或资源同步. 2.线程通信 线程通信,又叫进程内通信, ...

  9. Jmeter配置元件

    1.CSV Data Set Config Filename   参数化文件的路径 文件中的数据最后一行不能有空行,空行会被当做一个参数 若要进行分布式压测,可以将参数化文件放在jmeter的bin目 ...

  10. CSS中px,em,rem,pt的区别及四者换算?

    本文章重要说明px,em,rem,pt的区别以及四者之间的换算. em单位有如下特点 1. em的值并不是固定的; 2. em会继承父级元素的字体大小. 我们在写CSS的时候如果要用em为单位,需要注 ...