志,敢教日月换新天。为有牺牲多壮

[Swift]LeetCode887. 鸡蛋掉落 | Super Egg Drop

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

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

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

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

You are given K eggs, and you have access to a building with N floors from 1 to N

Each egg is identical in function, and if an egg breaks, you cannot drop it again.

You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break.

Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N). 

Your goal is to know with certainty what the value of F is.

What is the minimum number of moves that you need to know with certainty what Fis, regardless of the initial value of F

Example 1:

Input: K = 1, N = 2
Output: 2
Explanation: 
Drop the egg from floor 1.  If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2.  If it breaks, we know with certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Hence, we needed 2 moves in the worst case to know what F is with certainty.

Example 2:

Input: K = 2, N = 6
Output: 3

Example 3:

Input: K = 3, N = 14
Output: 4 

Note:

  1. 1 <= K <= 100
  2. 1 <= N <= 10000

你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N  共有 N 层楼的建筑。

每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去。

你知道存在楼层 F ,满足 0 <= F <= N 任何从高于 F 的楼层落下的鸡蛋都会碎,从 F 楼层或比它低的楼层落下的鸡蛋都不会破。

每次移动,你可以取一个鸡蛋(如果你有完整的鸡蛋)并把它从任一楼层 X 扔下(满足 1 <= X <= N)。

你的目标是确切地知道 F 的值是多少。

无论 F 的初始值如何,你确定 F 的值的最小移动次数是多少? 

示例 1:

输入:K = 1, N = 2
输出:2
解释:
鸡蛋从 1 楼掉落。如果它碎了,我们肯定知道 F = 0 。
否则,鸡蛋从 2 楼掉落。如果它碎了,我们肯定知道 F = 1 。
如果它没碎,那么我们肯定知道 F = 2 。
因此,在最坏的情况下我们需要移动 2 次以确定 F 是多少。

示例 2:

输入:K = 2, N = 6
输出:3

示例 3:

输入:K = 3, N = 14
输出:4 

提示:

  1. 1 <= K <= 100
  2. 1 <= N <= 10000

Runtime: 12 ms
Memory Usage: 18.7 MB
复制代码
 1 class Solution {
 2     func superEggDrop(_ K: Int, _ N: Int) -> Int {
 3         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:K + 1),count:N + 1)
 4         var m:Int = 0
 5         while(dp[m][K] < N)
 6         {
 7             m += 1
 8             for k in 1...K
 9             {
10                 dp[m][k] = dp[m - 1][k - 1] + dp[m - 1][k] + 1
11             }
12         }
13         return m
14     }
15 }
复制代码

Runtime: 24 ms
Memory Usage: 19 MB
复制代码
 1 class Solution {
 2     var mark = [String : Int]()
 3     func superEggDrop(_ K: Int, _ N: Int) -> Int {
 4         var step = 1
 5         while reachHeight(K, step) < N {
 6             step += 1
 7         }
 8         return step
 9     }
10     
11     func reachHeight(_ K: Int, _ N: Int) -> Int {
12         let key = "\(K),\(N)"
13         if let result = mark[key] {
14             return result
15         }
16         if K == 0 || N == 0 {
17             return 0
18         }
19         if K == 1 || N == 1 {
20             return N
21         }
22         var height = N
23         for i in 1..<N {
24             height += reachHeight(K - 1, i)
25         }
26         mark[key] = height
27         return height
28     }
29 }
复制代码

 

posted @   为敢技术  阅读(359)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°