• 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:

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

    类似Climbing Stairs.

  • 相关阅读:
    GXPT(一)——UI设计
    JVM系列文章(四):类载入机制
    poj 2688 状态压缩dp解tsp
    ASP.NET MVC Model绑定(四)
    cocos2dx实例开发之flappybird(入门版)
    qt creator中使用qwt插件
    [CodeEdit--Sublime]一些好用的Plugins
    NBUT 1225 NEW RDSP MODE I
    [IOC]Unity使用
    [Js/Jquery]jquery插件开发
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11490953.html
Copyright © 2020-2023  润新知