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 <= 10000
1 <= W <= 10000
- Answers will be accepted as correct if they are within
10^-5
of 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:
1 class Solution { 2 public double new21Game(int N, int K, int W) { 3 if(K == 0 || K-1+W <= N){ 4 return 1; 5 } 6 7 if(K > N){ 8 return 0; 9 } 10 11 double [] dp = new double[N+1]; 12 dp[0] = 1.0; 13 double winSum = 1; 14 15 double res = 0.0; 16 for(int i = 1; i<=N; i++){ 17 dp[i] = winSum/W; 18 19 if(i<K){ 20 winSum += dp[i]; 21 }else{ 22 res += dp[i]; 23 } 24 25 if(i >= W){ 26 winSum -= dp[i-W]; 27 } 28 } 29 30 return res; 31 } 32 }
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11490953.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android自定义钟表特效
- Android MediaPlayer 音频倍速播放 调整播放速度问题
- Android 简单实现倒计时功能
- Android Canvas自定义实现时钟效果
- 虚拟机kali安装vmtools
- Android动画学习笔记之补间动画
- [- Flutter 数据&状态篇 -] setState
- flutter 自定义websocket路由的实现
- Flutter开发之路由与导航的实现
- Android BSearchEdit 搜索结果选择框的实例代码
- 使用AccessibilityService实现微信自动切换账号功能
- Android评分RationBar控件使用详解
- Flutter里面错误捕获的正确方法
- django3 websockets
- 使用AccessibilityService实现自动遍历点赞功能