Java实现 LeetCode 837 新21点(DP)
837. 新21点
爱丽丝参与一个大致基于纸牌游戏 “21点” 规则的游戏,描述如下:
爱丽丝以 0 分开始,并在她的得分少于 K 分时抽取数字。 抽取时,她从 [1, W] 的范围中随机获得一个整数作为分数进行累计,其中 W 是整数。 每次抽取都是独立的,其结果具有相同的概率。
当爱丽丝获得不少于 K 分时,她就停止抽取数字。 爱丽丝的分数不超过 N 的概率是多少?
示例 1:
输入:N = 10, K = 1, W = 10
输出:1.00000
说明:爱丽丝得到一张卡,然后停止。
示例 2:
输入:N = 6, K = 1, W = 10
输出:0.60000
说明:爱丽丝得到一张卡,然后停止。
在 W = 10 的 6 种可能下,她的得分不超过 N = 6 分。
示例 3:
输入:N = 21, K = 17, W = 10
输出:0.73278
提示:
0 <= K <= N <= 10000
1 <= W <= 10000
如果答案与正确答案的误差不超过 10^-5,则该答案将被视为正确答案通过。
此问题的判断限制时间已经减少。
class Solution {
public double new21Game(int N, int K, int W) {
double res = 0;
int bound = (N<K+W)?N:K+W;
double dp[] = new double[bound+1];
double p = (double) 1 / W;
dp[0] = 1;
int lower = 0;
double acc = 0;
for(int i = 1; i <= bound; i++){
if(lower < i-W){
acc -= dp[lower];
lower++;
}
if(i<=K){
acc += dp[i-1];
}
dp[i] = acc * p;
}
for(int q = K; q <= bound; q++){
res += dp[q];
}
return res;
}
}
注意:最后送大家十套2020最新Java架构实战教程+大厂面试题库,进裙 783802103 在裙文件下载一起交流进步哦!
Java实现 LeetCode 837 新21点(DP)的更多相关文章
- LeetCode 837. New 21 Game
原题链接在这里:https://leetcode.com/problems/new-21-game/ 题目: Alice plays the following game, loosely based ...
- Java for LeetCode 108 Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...
- java高并发系列 - 第21天:java中的CAS操作,java并发的基石
这是java高并发系列第21篇文章. 本文主要内容 从网站计数器实现中一步步引出CAS操作 介绍java中的CAS及CAS可能存在的问题 悲观锁和乐观锁的一些介绍及数据库乐观锁的一个常见示例 使用ja ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- MySQL——关于索引的总结
索引的优缺点 首先说说索引的优点:最大的好处无疑就算提高查询效率.有的索引还能保证数据的唯一性,比如唯一索引. 而它的坏处很明显:索引也是文件,我们在创建索引时,也会创建额外的文件,所以会占用一些硬盘 ...
- 01背包问题,dp和贪心解法(c++11)
dp解法: 令dp[i]表示容量为i的背包所能得到的最大价值,考虑在当前物品集合中加入1个新考虑的物品i,则有如下状态转移方程:dp[j] = max(dp[j], dp[j - weight[i]] ...
- JS理论-:一只tom猫告诉你构造函数 实例 实例原型 实例原型的实例原型是什么
参考地址:https://github.com/mqyqingfeng/Blog/issues/2 感谢这位大佬 下面说说我的理解: 第一,看下人物: tom--一只叫tom的猫 Cat()--猫的构 ...
- 散列表PTA判断
1-1 在散列表中,所谓同义词就是具有相同散列地址的两个元素. (1分) T F 作者 DS课程组 单位 浙江大学 1-2 采用平方探测冲突解决策略(hi(k)=(H(k)+ ...
- Spring全家桶之springMVC(一)
Spring MVC简介和第一个spring MVC程序 Spring MVC是目前企业中使用较多的一个MVC框架,被很多业内人士认为是一个教科书级别的MVC表现层框架,Spring MVC是大名鼎鼎 ...
- 关于SpringMVC乱码问题
关于SpringMVC运行Tomcat控制台出现乱码的情况(在网上找到一种方法亲测有效) 找到tomcat文件夹中的conf包下的logging.properties中找到 java.util.log ...
- IOS真机测试(已拥有个人开发者证书)
创建真机调试证书并进行真机测试 步骤1 在启动台中点击其他,找到钥匙串访问. 步骤2 在打开的界面中点击右边的系统根证书,然后点击左上角的钥匙串访问,然后是证书助理,最后点击从证书颁发机构申请证书. ...
- mysql安装之后需要调的参数
http://www.mysqlperformanceblog.com/2014/01/28/10-mysql-settings-to-tune-after-installation/ 翻译加深理解. ...
- java十大排序
0.1 算法分类 十种常见排序算法可以分为两大类: 比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此也称为非线性时间比较类排序. 非比较类排序:不通过比较来决 ...
- mysql小白系列_05 日常操作
mysql启动/关闭 my.cnf的调用顺序 [root@docker02 bin]# ./mysql --help Default options are read from the followi ...