• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode808.分汤|SoupServings

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10550126.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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:

  1. Serve 100 ml of soup A and 0 ml of soup B
  2. Serve 75 ml of soup A and 25 ml of soup B
  3. Serve 50 ml of soup A and 50 ml of soup B
  4. Serve 25 ml of soup A and 75 ml of soup B

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.

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. 

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:

  • 0 <= N <= 10^9
  • Answers within 10^-6 of the true value will be accepted as correct.

有 A 和 B 两种类型的汤。一开始每种类型的汤有 N 毫升。有四种分配操作:

  1. 提供 100ml 的汤A 和 0ml 的汤B。
  2. 提供 75ml 的汤A 和 25ml 的汤B。
  3. 提供 50ml 的汤A 和 50ml 的汤B。
  4. 提供 25ml 的汤A 和 75ml 的汤B。

当我们把汤分配给某人之后,汤就没有了。每个回合,我们将从四种概率同为0.25的操作中进行分配选择。如果汤的剩余量不足以完成某次操作,我们将尽可能分配。当两种类型的汤都分配完时,停止操作。

注意不存在先分配100 ml汤B的操作。

需要返回的值: 汤A先分配完的概率 + 汤A和汤B同时分配完的概率 / 2。

示例:
输入: 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 的范围将被认为是正确的。


Runtime: 12 ms
Memory Usage: 19 MB
 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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
《Swift 权威指南》——导读发布时间:2022-07-13
下一篇:
swift学习笔记之-类和结构体发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap