给定N 个鸡蛋和K 个楼层,任务是找到所需的最少试验次数,在最坏的情况下,找到所有楼层都安全的楼层。如果从地板上掉下鸡蛋不会打破鸡蛋,地板是安全的。请参考 n 个鸡蛋和 k 个楼层以获取更多信息。
例子:
Input: N = 2, K = 10
Output: 4
Explanation:
The first trial was on the 4th floor. Two cases arise after this:
- If the egg breaks, we have one egg left, so we need three more trials.
- If the egg does not break, the next try is from the 7th floor. Again, two cases arise.
Input: N = 2, K = 100
Output: 14
先决条件:鸡蛋掉落拼图
方法:换个角度考虑这个问题:
让dp[x][n]是给定n 个鸡蛋和x 次移动可以检查的最大楼层数。
那么等式是:
- 如果鸡蛋破了,那么我们可以检查dp[x – 1][n – 1]层。
- 如果鸡蛋没有破,那么我们可以检查dp[x – 1][n] + 1层。
由于我们需要覆盖k层,因此dp[x][n] >= k。
dp[x][n]类似于组合的数量,它以指数方式增加到k
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum number
// of trials needed in the worst case
// with n eggs and k floors
int eggDrop(int n, int k)
{
vector > dp(k + 1, vector(n + 1, 0));
int x = 0;
// Fill all the entries in table using
// optimal substructure property
while (dp[x][n] < k) {
x++;
for (int i = 1; i <= n; i++)
dp[x][i] = dp[x - 1][i - 1] + dp[x - 1][i] + 1;
}
// Return the minimum number of moves
return x;
}
// Driver code
int main()
{
int n = 2, k = 36;
cout << eggDrop(n, k);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the minimum number
// of trials needed in the worst case
// with n eggs and k floors
static int eggDrop(int n, int k)
{
int dp[][] = new int [k + 1][n + 1];
int x = 0;
// Fill all the entries in table using
// optimal substructure property
while (dp[x][n] < k)
{
x++;
for (int i = 1; i <= n; i++)
dp[x][i] = dp[x - 1][i - 1] +
dp[x - 1][i] + 1;
}
// Return the minimum number of moves
return x;
}
// Driver code
public static void main(String args[])
{
int n = 2, k = 36;
System.out.println( eggDrop(n, k));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python implementation of the approach
# Function to return the minimum number
# of trials needed in the worst case
# with n eggs and k floors
def eggDrop(n, k):
dp = [[0 for i in range(n + 1)] for
j in range(k + 1)]
x = 0;
# Fill all the entries in table using
# optimal substructure property
while (dp[x][n] < k):
x += 1;
for i in range(1, n + 1):
dp[x][i] = dp[x - 1][i - 1] + \
dp[x - 1][i] + 1;
# Return the minimum number of moves
return x;
# Driver code
if __name__ == '__main__':
n = 2; k = 36;
print(eggDrop(n, k));
# This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum number
// of trials needed in the worst case
// with n eggs and k floors
static int eggDrop(int n, int k)
{
int [,]dp = new int [k + 1, n + 1];
int x = 0;
// Fill all the entries in table using
// optimal substructure property
while (dp[x, n] < k)
{
x++;
for (int i = 1; i <= n; i++)
dp[x, i] = dp[x - 1, i - 1] +
dp[x - 1, i] + 1;
}
// Return the minimum number of moves
return x;
}
// Driver code
public static void Main(String []args)
{
int n = 2, k = 36;
Console.WriteLine(eggDrop(n, k));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
8
时间复杂度: O(NlogK)
空间复杂度: O(N * K)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。