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 ...
随机推荐
- C++内存管理学习笔记(3)
/****************************************************************/ /* 学习是合作和分享式的! /* Auth ...
- docker build报错
docker build 遇到问题 "can not stat ... APPData\Local\Application Data" 解决方法:
- mysql和oracle 关于多表join的区别
http://stackoverflow.com/questions/10953143/join-performance-oracle-vs-mysql 翻译自上面的链接. Given a query ...
- 若依微服务版本 Windows下开发环境搭建
看了若依官网的教程,搭建环境还是踩了坑,简单整理一下 1.下载地址:https://gitee.com/y_project/RuoYi-Cloud 2.本地环境(仅供参考) JDK1.8 Mysql ...
- JS 如何获取自定义属性
<script>var testEle = document.getElementById("test"); testEle.setAttribute("de ...
- JavaScript(对象的创建模式)
JavaScript和其他语言略有不同,在JavaScript中,引用数据类型都是对象(包括函数).不过,在JavaScript中并没有“类”的概念,这决定了在JavaScript中不能直接来定义“类 ...
- poj3376 KMP+字典树求回文串数量(n*n)
Finding Palindromes Time Limit: 10000MS Memory Limit: 262144K Total Submissions: 4043 Accepted: ...
- 控制层技术:Servlet+reflection、Struts2、Spring MVC三者之间的比较学习
Servlet Struts2 Spring MVC 处理用户提交的数据 基于MVC设计模式的Web应用程序 是一个框架 是MVC框架 导入servlet包,配置web.xml文件 web.xml & ...
- 26-13 order by排序
表中数据是集合,集合是没有顺序的.order by返回的数据是有顺序的,故此我们把order by以后返回的数据集合叫“游标”. --------------------------通过order b ...
- Liquibase使用小结
简介 Liquibase是一个用于跟踪.管理和应用数据库变化的开源数据库重构工具.它将所有数据库的变化保存在XML文件中,便于版本控制和项目部署升级.在快速搭建项目的JHipster框架中集成了该工具 ...