博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 837. New 21 Game
阅读量:4621 次
发布时间:2019-06-09

本文共 2384 字,大约阅读时间需要 7 分钟。

原题链接在这里:

题目:

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 = 10Output: 1.00000Explanation:  Alice gets a single card, then stops.

Example 2:

Input: N = 6, K = 1, W = 10Output: 0.60000Explanation:  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 = 10Output: 0.73278

Note:

  1. 0 <= K <= N <= 10000
  2. 1 <= W <= 10000
  3. Answers will be accepted as correct if they are within 10^-5 of the correct answer.
  4. 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
= W){26 winSum -= dp[i-W];27 }28 }29 30 return res;31 }32 }

类似.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11490953.html

你可能感兴趣的文章
Python数据类型之“数字(numerics)”
查看>>
zoj2319Beautiful People Dp
查看>>
图片加载 背景色块问题
查看>>
Static Binding (Early Binding) vs Dynamic Binding (Late Binding)
查看>>
搭建git服务器
查看>>
MYSQL explain详解
查看>>
iOS之UIDynamic UI动力学使用步骤
查看>>
poj 2498 动态规划
查看>>
Windows Phone 7中使用PhoneApplicationService类保存应用程序状态
查看>>
MySql数据库的下载和安装卸载
查看>>
JDBC接口核心的API
查看>>
双缓冲技术局部更新原理之派生自View
查看>>
PPAPI插件与浏览器的通信
查看>>
用 query 方法 获得xml 节点的值
查看>>
Hello,Android
查看>>
Sublime Text 3 build 3103 注册码
查看>>
删与改
查看>>
SAP 中如何寻找增强
查看>>
spi驱动无法建立spidev问题
查看>>
ANDROID开发之SQLite详解
查看>>