LeetCode 837. New 21 Game
原题链接在这里: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:
0 <= K <= N <= 100001 <= W <= 10000- Answers will be accepted as correct if they are within
10^-5of the correct answer. - 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;
}
}
LeetCode 837. New 21 Game的更多相关文章
- Java实现 LeetCode 837 新21点(DP)
837. 新21点 爱丽丝参与一个大致基于纸牌游戏 "21点" 规则的游戏,描述如下: 爱丽丝以 0 分开始,并在她的得分少于 K 分时抽取数字. 抽取时,她从 [1, W] 的范 ...
- 【LeetCode】837. New 21 Game 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【leetcode】837. New 21 Game
题目如下: 解题思路:这个题目有点像爬楼梯问题,只不过楼梯问题要求的计算多少种爬的方式,但是本题是计算概率.因为点数超过或者等于K后就不允许再增加新的点数了,因此我们可以确定最终Alice拥有的点数的 ...
- 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 ...
- 【leetcode❤python】21. Merge Two Sorted Lists
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- Leetcode题解(21)
62. Unique Paths 题目 分析: 机器人一共要走m+n-2步,现在举个例子类比,有一个m+n-2位的二进制数,现在要在其中的m位填0,其余各位填1,一共有C(m+n-2,m-1)种可能, ...
- [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 ...
- Leetcode题库——21.合并两个有序链表
@author: ZZQ @software: PyCharm @file: mergeTwoLists.py @time: 2018/9/16 20:49 要求:将两个有序链表合并为一个新的有序链表 ...
- LeetCode记录之21——Merge Two Sorted Lists
算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...
随机推荐
- Delphi快递鸟【支持快递查询和单号识别】
作者QQ:(648437169) 点击下载➨Delphi快递鸟 [delphi快递鸟]支持快递查询.单号识别.
- Vue框架(二)——Vue指令(v-once指令、v-cloak指令、条件指令、v-pre指令、循环指令)、todolist案例、Vue实例(计算、监听)、组件、组件数据交互
Vue指令 1.v-once指令 单独使用,限制的标签内容一旦赋值,便不可被动更改(如果是输入框,可以主动修改) <!DOCTYPE html> <html lang=" ...
- 阿里云 轻量应用服务器 上传一个HTML文件或者jsp文件 通过外网IP访问
我选择的是 宝塔Linux,现在就说一下如何往服务器里面传文件然后通过外网IP访问 1.打开宝塔Linux面板登录,安装tomcat,安装好后如图 2.点击tomcat有个文件标识处 3.出现如图 4 ...
- ubuntu系统下防火墙简单使用
apt-get install ufw 安装防火墙sudo ufw enable|disable|status 开启/关闭/查看防火墙状态sudo ufw allow 22/ ...
- MOOC C++笔记(六):多态
多态 虚函数 在类的定义中,前面有virtual关键字的成员函数就是虚函数. virtual关键字只用在类定义里的函数声明中,写函数体时不用. 构造函数和静态成员函数不能是虚函数. 多态的表现形式 基 ...
- 《明日方舟》Python版公开招募工具
工具介绍 根据输入的标签,快速找出能够招募4星,5星干员的标签组合,比如刷出了 重装 | 男 | 支援 |术师 | 先锋 五个标签,输入效果如下: 注意:不支持高级干员和资深高级干员标签 使用环境 安 ...
- pandas-09 pd.groupby()的用法
pandas-09 pd.groupby()的用法 在pandas中的groupby和在sql语句中的groupby有异曲同工之妙,不过也难怪,毕竟关系数据库中的存放数据的结构也是一张大表罢了,与da ...
- 【转载】C#中使用decimal.TryParse方法将字符串转换为十进制decimal类型
在C#编程过程中,将字符串string转换为decimal类型过程中,时常使用decimal.Parse方法,但decimal.Parse在无法转换的时候,会抛出程序异常,其实还有个decimal.T ...
- Java 之 JSP
一.JSP 概述 Java Server Pages:java 服务器页面.页面中既可以指定定义 html标签,也可以定义 Java 代码. 二.原理 JSP 本质上就是一个 Servlet. 原理示 ...
- Celery:Daemonization
参考文档:http://docs.celeryproject.org/en/latest/userguide/daemonizing.html#daemonizing