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)的更多相关文章

  1. LeetCode 837. New 21 Game

    原题链接在这里:https://leetcode.com/problems/new-21-game/ 题目: Alice plays the following game, loosely based ...

  2. 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. 解题 ...

  3. java高并发系列 - 第21天:java中的CAS操作,java并发的基石

    这是java高并发系列第21篇文章. 本文主要内容 从网站计数器实现中一步步引出CAS操作 介绍java中的CAS及CAS可能存在的问题 悲观锁和乐观锁的一些介绍及数据库乐观锁的一个常见示例 使用ja ...

  4. 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 ...

  5. 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. ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. lammps 学习之:系统压力太大,导致原子丢失

    体系压力太大:146981.52bar,体系压强太大 会把原子挤跑 出现原子丢失的情况(lost atoms). 原子丢失: 解决方法:增大体系体积.增加z方向的距离.

  2. 第六章第二十题(计算一个字符串中字母的个数)(Count the letters in a string) - 编程练习题答案

    *6.20(计算一个字符串中字母的个数)编写一个方法,使用下面的方法头计算字符串中的字母个数: public static int countLetters(String s) 编写一个测试程序,提示 ...

  3. x86软路由虚拟化openwrt-koolshare-mod-v2.33联通双拨IPV6教程(第一篇)

    本文分两篇发布,此为第一篇,第二篇:https://www.cnblogs.com/zlAurora/p/12433302.html   年前TB购置了一台软路由,对家里网络来了个大改造,实现了PPP ...

  4. asp.net core计划任务探索之hangfire+redis+cluster

    研究了一整天的quartz.net,发现一直无法解决cluster模式下多个node独立运行的问题,改了很多配置项,仍然是每个node各自为战.本来cluster模式下的各个node应该是负载均衡的, ...

  5. [hdu5416 CRB and Tree]树上路径异或和,dfs

    题意:给一棵树,每条边有一个权值,求满足u到v的路径上的异或和为s的(u,v)点对数 思路:计a到b的异或和为f(a,b),则f(a,b)=f(a,root)^f(b,root).考虑dfs,一边计算 ...

  6. 黑马vue学习的总结,vue笔记

    cls:清除终端输出 $refs $http $route 使用this.$emit('show')来调用父方法

  7. yield与park的区别

    yield表示放弃本次cpu的时间片,但是操作系统在下一个时间片依旧可能会调用该线程/进程 park表示线程/进程睡眠,需要让其他线程/进程唤醒,才有可能重新被操作系统分配时间片, 非自旋锁,底层一般 ...

  8. zz 通过INFORMATION_SCHEMA.INNODB_TRX、INNODB_LOCKS、INNODB_LOCK_WAITS 三个表获取事务与锁的信息

    zz from http://imysql.com/2015/03/25/mysql-faq-how-to-fetch-latest-trxid.shtml #先查询 INNODB_TRX 表,看看都 ...

  9. C# 数据操作系列 - 13 SugarSql初探

    0. 前言 前言,暂时挥别NHibernate(虽然我突然发现这玩意还挺有意思的,不过看得人不多).大步进入了有很多小伙伴向我安利的SQLSugar,嗯,我一直叫SugarSQL,好像是这个吧? 这是 ...

  10. 基于Javaee的影视创作论坛的设计与实现

    基于Javaee的影视创作论坛的设计与实现主要用功能包括: 首页推荐.用户管理.影片管理.评论管理. 预告片管理.海报管理.公告管理.数据检索.用户注册与登录等等功能.统结构如下 (1)后台管理: 管 ...