📜  鸡蛋掉拼图套装2

📅  最后修改于: 2021-05-06 20:04:28             🧑  作者: Mango

给定N个鸡蛋和K个地板,任务是找到最坏情况下所需的最少试验次数,以找到所有地板都安全的地板。如果从地板上扔鸡蛋不会破坏鸡蛋,则地板是安全的。请参考n个鸡蛋和k个楼层,以获取更多信息。

例子:

先决条件:鸡蛋丢拼图

方法:以另一种方式考虑此问题:
dp [x] [n]是在给定n个鸡蛋和x个移动的情况下可以检查的最大楼层数。

那么等式为:

    dp [x] [n] = dp [x – 1] [n – 1] + dp [x – 1] [n] + 1
    这意味着我们需要1点移动到地板上,
  1. 如果鸡蛋破裂了,那么我们可以检查dp [x – 1] [n – 1]底数。
  2. 如果鸡蛋没有破裂,那么我们可以检查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


输出:
8

时间复杂度: O(NlogK)
空间复杂度: O(N * K)