★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-ybwluxyv-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
There are two types of soup: type A and type B. Initially we have N
ml of each type of soup. There are four kinds of operations:git
When we serve some soup, we give it to someone and we no longer have it. Each turn, we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as we can. We stop once we no longer have some quantity of both types of soup.github
Note that we do not have the operation where all 100 ml's of soup B are used first. 微信
Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time. spa
Example: Input: N = 50 Output: 0.625 Explanation: If we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
Notes:code
0 <= N <= 10^9
. 10^-6
of the true value will be accepted as correct.有 A 和 B 兩種類型的湯。一開始每種類型的湯有 N
毫升。有四種分配操做:htm
當咱們把湯分配給某人以後,湯就沒有了。每一個回合,咱們將從四種機率同爲0.25的操做中進行分配選擇。若是湯的剩餘量不足以完成某次操做,咱們將盡量分配。當兩種類型的湯都分配完時,中止操做。blog
注意不存在先分配100 ml湯B的操做。rem
須要返回的值: 湯A先分配完的機率 + 湯A和湯B同時分配完的機率 / 2。get
示例: 輸入: N = 50 輸出: 0.625 解釋: 若是咱們選擇前兩個操做,A將首先變爲空。對於第三個操做,A和B會同時變爲空。對於第四個操做,B將首先變爲空。 因此A變爲空的總機率加上A和B同時變爲空的機率的一半是 0.25 *(1 + 1 + 0.5 + 0)= 0.625。
註釋:
0 <= N <= 10^9
。返回值在 10^-6
的範圍將被認爲是正確的。
1 class Solution { 2 var m:[String:Double] = [String:Double]() 3 func soupServings(_ N: Int) -> Double { 4 return N >= 4800 ? 1.0 : f(N, N) 5 } 6 7 func f(_ a:Int,_ b:Int) ->Double 8 { 9 if a <= 0 && b <= 0 {return 0.5} 10 if a <= 0 {return 1.0} 11 if b <= 0 {return 0} 12 var spoon:String = String(a) + ":" + String(b) 13 if m[spoon] == nil 14 { 15 m[spoon] = 0.25 * (f(a - 100, b) + f(a - 75, b - 25) + f(a - 50, b - 50) + f(a - 25, b - 75)) 16 } 17 return m[spoon]! 18 } 19 }
72ms
1 class Solution { 2 var mark = [String : Double]() 3 func soupServings(_ N: Int) -> Double { 4 return N > 4800 ? 1 : distributeSoup(N, N) 5 } 6 7 func distributeSoup(_ a: Int, _ b: Int) -> Double { 8 let key = "\(a),\(b)" 9 if let rate = mark[key] { 10 return rate 11 } 12 13 if a <= 0 { 14 if b > 0 { 15 return 1 16 } else { 17 return 0.5 18 } 19 } else { 20 if b <= 0 { 21 return 0 22 } 23 } 24 25 let distribute = distributeSoup(a - 100, b) 26 + distributeSoup(a - 75, b - 25) 27 + distributeSoup(a - 50, b - 50) 28 + distributeSoup(a - 25, b - 75) 29 mark[key] = distribute * 0.25 30 return mark[key] ?? 0 31 } 32 }