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. 学习RxJava+Retrofit+OkHttp+MVP的网络请求使用

    公司的大佬用的是这一套,那我这个菜鸟肯定要学习使用了. 我在网上找了很多文章,写的都很详细,比如 https://www.jianshu.com/u/5fd2523645da https://www. ...

  2. 随笔 - B树算法实现

    写代码之前,再回顾一下B树是什么,满足什么样的规则 B树规则: 排序方式:所有节点关键字是按递增次序排列,并遵循左小右大原则 子节点数:非叶节点的子节点数>1,且<=M ,且M>=2 ...

  3. PI/PO Token配置

    接收方通道配置 因为本例中需要在访问业务接口时,获取某平台的TOKEN认证,并在调用业务接口时,将TOKEN一同传给某平台,但是不能放在请求参数中,而是放在HTTP的Headers 注意!是Heade ...

  4. mybatis分页助手分页

    一.编写dao及配置文件 (1)配置文件两种方式 第一种: <!--SqlSeesionFactoryBean对象--><bean id="sqlSessionFactor ...

  5. struts2 redirectaction

    <result type="redirectAction"> <param name="actionName">dashboard< ...

  6. java中关于对象的可达可用问题

    (注:本文引用知识纯粹为技术交流,未经允许不可私自转载)Java中其实也有内存泄露,就是因为对象无用却可达的原因.这个细分细分下来有三个1. 不可用不可达------>这种情况GC会帮我们回收掉 ...

  7. 搜索引擎优化(SEO)

    一.SEM SEM(Search Engine Marketing)即搜索引擎营销.SEM是一种新的网络营销模式.SEM所做的就是全面有效地利用所搜引擎来进行网络行销推广.SEM追求最高的性价比,以最 ...

  8. Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

    一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...

  9. 不会看 Explain执行计划,劝你简历别写熟悉 SQL优化

    昨天中午在食堂,和部门的技术大牛们坐在一桌吃饭,作为一个卑微技术渣仔默默的吃着饭,听大佬们高谈阔论,研究各种高端技术,我TM也想说话可实在插不上嘴. 聊着聊着突然说到他上午面试了一个工作6年的程序员, ...

  10. django之CORS跨域请求

    对于想要利用django框架实现前后端分离,首要的问题是解决跨域请求的问题,什么是跨域请求?简单来说就是当前发起的请求的域与该请求指向的资源所在的域不一致.当协议+域名+端口号均相同,那么就是同一个域 ...