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

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

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

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

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). less

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

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

Example 1:spa

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

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

Example 3:htm

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 }
相關文章
相關標籤/搜索