[Swift]LeetCode837. 新21點 | New 21 Game

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-etyabxps-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Alice plays the following game, loosely based on the card game "21".git

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

Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points?微信

Example 1:less

Input: N = 10, K = 1, W = 10
Output: 1.00000
Explanation:  Alice gets a single card, then stops.

Example 2:dom

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

Input: N = 21, K = 17, W = 10
Output: 0.73278

Note:spa

  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.

愛麗絲參與一個大體基於紙牌遊戲 「21點」 規則的遊戲,描述以下:code

愛麗絲以 0 分開始,並在她的得分少於 K分時抽取數字。 抽取時,她從 [1, W] 的範圍中隨機得到一個整數做爲分數進行累計,其中 W 是整數。 每次抽取都是獨立的,其結果具備相同的機率。htm

當愛麗絲得到很多於 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

提示:

  1. 0 <= K <= N <= 10000
  2. 1 <= W <= 10000
  3. 若是答案與正確答案的偏差不超過 10^-5,則該答案將被視爲正確答案經過。
  4. 此問題的判斷限制時間已經減小。

24ms

 1 class Solution {
 2     func new21Game(_ N: Int, _ K: Int, _ W: Int) -> Double {
 3         guard N > 0 else { return 1.0 }
 4         guard W >= N - K else { return 1.0 }
 5         var dp = Array(repeating: 0.0, count: N+1)
 6         dp[0] = 1
 7         let w = Double(W)
 8         var temp = 0.0
 9         for i in 1...N {
10             if i <= K {
11               temp += dp[i-1] / w  
12             }
13             if i > W {
14                 temp -= dp[i-W-1] / w
15             } 
16             dp[i] = temp  
17         }
18         return dp[K...].reduce(0.0, +)
19     }
20 }

Runtime: 48 ms
Memory Usage: 18.8 MB
 1 class Solution {
 2     func new21Game(_ N: Int, _ K: Int, _ W: Int) -> Double {        
 3         if K == 0 || N >= (K + W) {return 1.0}        
 4         var dp:[Double] = [Double](repeating:0.0,count:K + W)
 5         dp[0] = 1.0
 6         for i in 1..<(K + W)
 7         {
 8             dp[i] = dp[i - 1]
 9             if i <= W
10             {
11                 dp[i] += dp[i - 1] / Double(W)
12             }
13             else
14             {
15                 dp[i] += (dp[i - 1] - dp[i - W - 1]) / Double(W)
16             }
17             if i > K
18             {
19                 dp[i] -= (dp[i - 1] - dp[K - 1]) / Double(W)            
20             }            
21         }
22         return dp[N] - dp[K - 1]
23     }
24 }
相關文章
相關標籤/搜索