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
算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...
随机推荐
- Go基础编程实践(八)—— 系统编程
捕捉信号 // 运行此程序,控制台将打印"Waiting for signal" // 按Ctrl + C 发送信号以关闭程序,将发生中断 // 随后控制台依次打印"Si ...
- 【读书笔记】胡说IC
- todolist 包含本地存储知识
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MMKV 多进程K-V组件 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- IDEA 2019 快捷键终极大全
常用的有fori/sout/psvm+Tab即可生成循环.System.out.main方法等boilerplate样板代码 . 例如要输入for(User user : users) 只需输入use ...
- CentOS7配置网卡上网、安装wget、配置163yum源
2019/09/12,CentOS 7 VMware 摘要:CentOS7安装完成(最小化安装)后,不能联网(已选择桥接网络),需要修改配置文件及配置yum源 修改配置文件 进入网卡配置目录 cd / ...
- virsh 添加虚拟交换机
virsh 添加虚拟交换机 来源 https://blog.csdn.net/a1987463004/article/details/90905981 vim /etc/libvirt/qemu/ne ...
- tf.reduce_max的运用
a=np.array([[[[1],[2],[3]],[[4],[25],[6]]],[[[27],[8],[99]],[[10],[11],[12]]],[[[13],[14],[15]],[[16 ...
- P1108 低价购买 (DP)
题目 P1108 低价购买 解析 这题做的我身心俱惫,差点自闭. 当我WA了N发后,终于明白了这句话的意思 当二种方案"看起来一样"时(就是说它们构成的价格队列一样的时候),这2种 ...
- 【imx6ul应用开发】如何修改串口?
4.1如何修改串口?答:开发板已经调好了串口驱动,调试串口,只需要修改dts文件即可,客户可以根据实际需要,确定硬件管脚具体用哪一个. 打开内核源代码/arch/arm/boot/dts/myb-y6 ...