📜  鸡蛋丢拼图| DP-11

📅  最后修改于: 2021-04-29 06:42:30             🧑  作者: Mango

以下是对这个著名难题的实例的描述,该难题涉及n = 2个鸡蛋和k = 36层的建筑物。
假设我们希望知道36层建筑物中的哪个楼层可以安全地放下鸡蛋,并且哪些会导致鸡蛋在着陆时破裂。我们做一些假设:
…..跌落下来的鸡蛋可以再次使用。
…..破损的鸡蛋必须丢弃。
…..跌落对所有鸡蛋的影响都是相同的。
…..如果鸡蛋掉落时破裂,那么如果从较高的地板掉落,鸡蛋也会破裂。
…..如果鸡蛋在跌落中幸存下来,那么它在较短的跌落中幸存下来。
…..不能排除一楼的窗户破鸡蛋,也不可以排除第36层的窗户不会破鸡蛋。
如果只有一个鸡蛋,并且我们希望确保获得正确的结果,则只能以一种方式进行实验。从一楼的窗户放鸡蛋;如果它仍然存在,请将其从二楼窗口中放下。继续向上直到破裂。在最坏的情况下,此方法可能需要丢弃36次。假设有2个鸡蛋。保证在所有情况下都能正常工作的最少的排卵次数是多少?
问题实际上并不是找到关键的最低限度,而仅仅是确定应该从中跌落鸡蛋的最低限度,以使试验的总次数减至最少。
资料来源:动态编程维基

方法1递归。
在本文中,我们将讨论“ n”个鸡蛋和“ k”个地板的一般问题的解决方案。解决方案是尝试从每个楼层(从1到k)投下一个鸡蛋,然后递归计算最坏情况下所需的最小投掷次数。在最坏的情况下提供最小值的地板将成为解决方案的一部分。
在以下解决方案中,我们返回最坏情况下的最少试验次数;可以轻松修改这些解决方案,以打印每个试验的楼层编号。
最坏情况的含义:最坏情况为用户提供最低限度的保证。例如-如果我们有’1’鸡蛋和’k’地板,我们将从一楼开始放鸡蛋,直到假设鸡蛋在’kth’地板上破裂为止,因此向我们提供保证的尝试次数为’k’ 。
1)最佳子结构:
当我们从地板x上放一个鸡蛋时,可能有两种情况(1)鸡蛋破裂(2)鸡蛋没有破裂。

  1. 如果鸡蛋从’xth’地板掉落后破裂,那么我们只需要检查剩余鸡蛋高度低于’x’的地板,因为某些地板应该存在低于’x’且鸡蛋不会破裂的位置;因此问题减少到x-1层和n-1个鸡蛋。
  2. 如果鸡蛋从’xth’地板掉下来后没有破裂,那么我们只需要检查高于’x’的地板即可。因此问题减少到“ kx”层和n个鸡蛋。

由于我们需要在最坏的情况下尽量减少试验次数,因此我们最多采用两种情况。我们考虑每个楼层的上述两种情况中的最大值,并选择产生最少试验次数的楼层。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// A utility function to get
// maximum of two integers
int max(int a, int b)
{
    return (a > b) ? a : b;
}
 
// Function to get minimum
// number of trials needed in worst
// case with n eggs and k floors
int eggDrop(int n, int k)
{
    // If there are no floors,
    // then no trials needed.
    // OR if there is one floor,
    // one trial needed.
    if (k == 1 || k == 0)
        return k;
 
    // We need k trials for one
    // egg and k floors
    if (n == 1)
        return k;
 
    int min = INT_MAX, x, res;
 
    // Consider all droppings from
    // 1st floor to kth floor and
    // return the minimum of these
    // values plus 1.
    for (x = 1; x <= k; x++) {
        res = max(
            eggDrop(n - 1, x - 1),
            eggDrop(n, k - x));
        if (res < min)
            min = res;
    }
 
    return min + 1;
}
 
// Driver program to test
// to pront printDups
int main()
{
    int n = 2, k = 10;
    cout << "Minimum number of trials "
            "in worst case with "
         << n << " eggs and " << k
         << " floors is "
         << eggDrop(n, k) << endl;
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C
#include 
#include 
 
// A utility function to get
// maximum of two integers
int max(int a, int b)
{
    return (a > b) ? a : b;
}
 
/* Function to get minimum number of
 trials needed in worst case with n eggs
 and k floors */
int eggDrop(int n, int k)
{
    // If there are no floors, then no
    // trials needed. OR if there is
    // one floor, one trial needed.
    if (k == 1 || k == 0)
        return k;
 
    // We need k trials for one egg and
    // k floors
    if (n == 1)
        return k;
 
    int min = INT_MAX, x, res;
 
    // Consider all droppings from 1st
    // floor to kth floor and
    // return the minimum of these values
    // plus 1.
    for (x = 1; x <= k; x++) {
        res = max(
            eggDrop(n - 1, x - 1),
            eggDrop(n, k - x));
        if (res < min)
            min = res;
    }
 
    return min + 1;
}
 
/* Driver program to test to pront printDups*/
int main()
{
    int n = 2, k = 10;
    printf("nMinimum number of trials in "
           "worst case with %d eggs and "
           "%d floors is %d \n",
           n, k, eggDrop(n, k));
    return 0;
}


Java
public class GFG {
 
    /* Function to get minimum number of
    trials needed in worst case with n
    eggs and k floors */
    static int eggDrop(int n, int k)
    {
        // If there are no floors, then
        // no trials needed. OR if there
        // is one floor, one trial needed.
        if (k == 1 || k == 0)
            return k;
 
        // We need k trials for one egg
        // and k floors
        if (n == 1)
            return k;
 
        int min = Integer.MAX_VALUE;
        int x, res;
 
        // Consider all droppings from
        // 1st floor to kth floor and
        // return the minimum of these
        // values plus 1.
        for (x = 1; x <= k; x++) {
            res = Math.max(eggDrop(n - 1, x - 1),
                           eggDrop(n, k - x));
            if (res < min)
                min = res;
        }
 
        return min + 1;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 2, k = 10;
        System.out.print("Minimum number of "
                         + "trials in worst case with "
                         + n + " eggs and " + k
                         + " floors is " + eggDrop(n, k));
    }
    // This code is contributed by Ryuga.
}


Python 3
import sys
 
# Function to get minimum number of trials
# needed in worst case with n eggs and k floors
def eggDrop(n, k):
 
    # If there are no floors, then no trials
    # needed. OR if there is one floor, one
    # trial needed.
    if (k == 1 or k == 0):
        return k
 
    # We need k trials for one egg
    # and k floors
    if (n == 1):
        return k
 
    min = sys.maxsize
 
    # Consider all droppings from 1st
    # floor to kth floor and return
    # the minimum of these values plus 1.
    for x in range(1, k + 1):
 
        res = max(eggDrop(n - 1, x - 1),
                  eggDrop(n, k - x))
        if (res < min):
            min = res
 
    return min + 1
 
# Driver Code
if __name__ == "__main__":
 
    n = 2
    k = 10
    print("Minimum number of trials in worst case with",
           n, "eggs and", k, "floors is", eggDrop(n, k))
 
# This code is contributed by ita_c


C#
using System;
 
class GFG {
 
    /* Function to get minimum number of
    trials needed in worst case with n
    eggs and k floors */
    static int eggDrop(int n, int k)
    {
        // If there are no floors, then
        // no trials needed. OR if there
        // is one floor, one trial needed.
        if (k == 1 || k == 0)
            return k;
 
        // We need k trials for one egg
        // and k floors
        if (n == 1)
            return k;
 
        int min = int.MaxValue;
        int x, res;
 
        // Consider all droppings from
        // 1st floor to kth floor and
        // return the minimum of these
        // values plus 1.
        for (x = 1; x <= k; x++) {
            res = Math.Max(eggDrop(n - 1, x - 1),
                           eggDrop(n, k - x));
            if (res < min)
                min = res;
        }
 
        return min + 1;
    }
 
    // Driver code
    static void Main()
    {
        int n = 2, k = 10;
        Console.Write("Minimum number of "
                      + "trials in worst case with "
                      + n + " eggs and " + k
                      + " floors is " + eggDrop(n, k));
    }
}
 
// This code is contributed by Sam007.


Javascript


C++
// A Dynamic Programming based for
// the Egg Dropping Puzzle
#include 
#include 
 
// A utility function to get
// maximum of two integers
int max(int a, int b)
{
    return (a > b) ? a : b;
}
 
/* Function to get minimum
number of trials needed in worst
case with n eggs and k floors */
int eggDrop(int n, int k)
{
    /* A 2D table where entery
    eggFloor[i][j] will represent
    minimum number of trials needed for
    i eggs and j floors. */
    int eggFloor[n + 1][k + 1];
    int res;
    int i, j, x;
 
    // We need one trial for one floor and 0
    // trials for 0 floors
    for (i = 1; i <= n; i++) {
        eggFloor[i][1] = 1;
        eggFloor[i][0] = 0;
    }
 
    // We always need j trials for one egg
    // and j floors.
    for (j = 1; j <= k; j++)
        eggFloor[1][j] = j;
 
    // Fill rest of the entries in table using
    // optimal substructure property
    for (i = 2; i <= n; i++) {
        for (j = 2; j <= k; j++) {
            eggFloor[i][j] = INT_MAX;
            for (x = 1; x <= j; x++) {
                res = 1 + max(
                              eggFloor[i - 1][x - 1],
                              eggFloor[i][j - x]);
                if (res < eggFloor[i][j])
                    eggFloor[i][j] = res;
            }
        }
    }
 
    // eggFloor[n][k] holds the result
    return eggFloor[n][k];
}
 
/* Driver program to test to pront printDups*/
int main()
{
    int n = 2, k = 36;
    printf("\nMinimum number of trials "
           "in worst case with %d eggs and "
           "%d floors is %d \n",
           n, k, eggDrop(n, k));
    return 0;
}


Java
// A Dynamic Programming based Java
// Program for the Egg Dropping Puzzle
class EggDrop {
 
    // A utility function to get
    // maximum of two integers
    static int max(int a, int b)
    {
        return (a > b) ? a : b;
    }
 
    /* Function to get minimum number
 of trials needed in worst
    case with n eggs and k floors */
    static int eggDrop(int n, int k)
    {
        /* A 2D table where entery eggFloor[i][j]
 will represent minimum number of trials
needed for i eggs and j floors. */
        int eggFloor[][] = new int[n + 1][k + 1];
        int res;
        int i, j, x;
 
        // We need one trial for one floor and
        // 0 trials for 0 floors
        for (i = 1; i <= n; i++) {
            eggFloor[i][1] = 1;
            eggFloor[i][0] = 0;
        }
 
        // We always need j trials for one egg
        // and j floors.
        for (j = 1; j <= k; j++)
            eggFloor[1][j] = j;
 
        // Fill rest of the entries in table using
        // optimal substructure property
        for (i = 2; i <= n; i++) {
            for (j = 2; j <= k; j++) {
                eggFloor[i][j] = Integer.MAX_VALUE;
                for (x = 1; x <= j; x++) {
                    res = 1 + max(
                                  eggFloor[i - 1][x - 1],
                                  eggFloor[i][j - x]);
                    if (res < eggFloor[i][j])
                        eggFloor[i][j] = res;
                }
            }
        }
 
        // eggFloor[n][k] holds the result
        return eggFloor[n][k];
    }
 
    /* Driver program to test to pront printDups*/
    public static void main(String args[])
    {
        int n = 2, k = 10;
        System.out.println("Minimum number of trials in worst"
                           + " case with "
                           + n + "  eggs and "
                           + k + " floors is " + eggDrop(n, k));
    }
}
/*This code is contributed by Rajat Mishra*/


Python
# A Dynamic Programming based Python Program for the Egg Dropping Puzzle
INT_MAX = 32767
 
# Function to get minimum number of trials needed in worst
# case with n eggs and k floors
def eggDrop(n, k):
    # A 2D table where entery eggFloor[i][j] will represent minimum
    # number of trials needed for i eggs and j floors.
    eggFloor = [[0 for x in range(k + 1)] for x in range(n + 1)]
 
    # We need one trial for one floor and0 trials for 0 floors
    for i in range(1, n + 1):
        eggFloor[i][1] = 1
        eggFloor[i][0] = 0
 
    # We always need j trials for one egg and j floors.
    for j in range(1, k + 1):
        eggFloor[1][j] = j
 
    # Fill rest of the entries in table using optimal substructure
    # property
    for i in range(2, n + 1):
        for j in range(2, k + 1):
            eggFloor[i][j] = INT_MAX
            for x in range(1, j + 1):
                res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x])
                if res < eggFloor[i][j]:
                    eggFloor[i][j] = res
 
    # eggFloor[n][k] holds the result
    return eggFloor[n][k]
 
# Driver program to test to pront printDups
n = 2
k = 36
print("Minimum number of trials in worst case with" + str(n) + "eggs and "
       + str(k) + " floors is " + str(eggDrop(n, k)))
 
# This code is contributed by Bhavya Jain


C#
// A Dynamic Programming based C# Program
// for the Egg Dropping Puzzle
using System;
 
class GFG {
 
    // A utility function to get maximum of
    // two integers
    static int max(int a, int b)
    {
        return (a > b) ? a : b;
    }
 
    /* Function to get minimum number of
    trials needed in worst case with n
    eggs and k floors */
    static int eggDrop(int n, int k)
    {
 
        /* A 2D table where entery eggFloor[i][j]
        will represent minimum number of trials
        needed for i eggs and j floors. */
        int[, ] eggFloor = new int[n + 1, k + 1];
        int res;
        int i, j, x;
 
        // We need one trial for one floor and0
        // trials for 0 floors
        for (i = 1; i <= n; i++) {
            eggFloor[i, 1] = 1;
            eggFloor[i, 0] = 0;
        }
 
        // We always need j trials for one egg
        // and j floors.
        for (j = 1; j <= k; j++)
            eggFloor[1, j] = j;
 
        // Fill rest of the entries in table
        // using optimal substructure property
        for (i = 2; i <= n; i++) {
            for (j = 2; j <= k; j++) {
                eggFloor[i, j] = int.MaxValue;
                for (x = 1; x <= j; x++) {
                    res = 1 + max(eggFloor[i - 1, x - 1],
                                  eggFloor[i, j - x]);
                    if (res < eggFloor[i, j])
                        eggFloor[i, j] = res;
                }
            }
        }
 
        // eggFloor[n][k] holds the result
        return eggFloor[n, k];
    }
 
    // Driver function
    public static void Main()
    {
        int n = 2, k = 36;
        Console.WriteLine("Minimum number of trials "
                          + "in worst case with " + n + " eggs and "
                          + k + "floors is " + eggDrop(n, k));
    }
}
 
// This code is contributed by Sam007.


PHP


C++
#include 
using namespace std;
#define MAX 1000
 
vector> memo(MAX, vector (MAX, -1));
int solveEggDrop(int n, int k) {
  
    if(memo[n][k] != -1) { return memo[n][k];}
     
    if (k == 1 || k == 0)
      return k;
 
    if (n == 1)
      return k;
 
    int min = INT_MAX, x, res;
 
    for (x = 1; x <= k; x++) {
      res = max(
        solveEggDrop(n - 1, x - 1),
        solveEggDrop(n, k - x));
      if (res < min)
        min = res;
    }
     
    memo[n][k] = min+1;
    return min + 1;
  }
 
int main() {
 
    int n = 2, k = 36;
    cout<


输出
Minimum number of trials in worst case with 2 eggs and 10 floors is 4

输出:

Minimum number of trials in worst 
case with 2 eggs and 10 floors is 4

应该注意的是,上述函数一次又一次地计算相同的子问题。请参阅下面的部分递归树,E(2,2)被评估两次。当您绘制完整的递归树时,即使对于较小的n和k值,也会有许多重复的子问题。

E(2, 4)
                           |                      
          ------------------------------------- 
          |             |           |         |   
          |             |           |         |       
      x=1/          x=2/      x=3/     x=4/ 
        /             /         ....      ....
       /             /    
 E(1, 0)  E(2, 3)     E(1, 1)  E(2, 2)
          /  /...         /  
      x=1/                 .....
        /    
     E(1, 0)  E(2, 2)
            /   
            ......

Partial recursion tree for 2 eggs and 4 floors.

复杂度分析:

  • 时间复杂度:由于存在子问题重叠的情况,因此时间复杂度是指数级的。
  • 辅助空间: O(1)。由于没有使用任何数据结构来存储值。

由于再次调用了相同的问题,因此此问题具有“重叠子问题”属性。因此,Egg Dropping Puzzle具有动态编程问题的两个属性(请参阅此内容)。像其他典型的动态编程(DP)问题一样,可以通过以自下而上的方式构造一个临时数组eggFloor [] []来避免相同子问题的重新计算。
方法2动态编程。
在这种方法中,我们采用与上述相同的想法,而忽略了一次又一次地计算子问题答案的情况。 。该方法将是创建一个表,该表将存储子问题的结果,以便解决子问题,只需要从该表中进行查找即可,这需要花费固定时间,而之前需要花费指数时间
正式填充DP [i] [j]状态,其中“ i”是鸡蛋数,“ j”是楼层数:

  • 我们必须从“ 1”到“ j”遍历每个楼层“ x”,并找出以下项中的最小值:
(1 + max( DP[i-1][j-1], DP[i][j-x] )).

该模拟将使情况变得清晰:

C++

// A Dynamic Programming based for
// the Egg Dropping Puzzle
#include 
#include 
 
// A utility function to get
// maximum of two integers
int max(int a, int b)
{
    return (a > b) ? a : b;
}
 
/* Function to get minimum
number of trials needed in worst
case with n eggs and k floors */
int eggDrop(int n, int k)
{
    /* A 2D table where entery
    eggFloor[i][j] will represent
    minimum number of trials needed for
    i eggs and j floors. */
    int eggFloor[n + 1][k + 1];
    int res;
    int i, j, x;
 
    // We need one trial for one floor and 0
    // trials for 0 floors
    for (i = 1; i <= n; i++) {
        eggFloor[i][1] = 1;
        eggFloor[i][0] = 0;
    }
 
    // We always need j trials for one egg
    // and j floors.
    for (j = 1; j <= k; j++)
        eggFloor[1][j] = j;
 
    // Fill rest of the entries in table using
    // optimal substructure property
    for (i = 2; i <= n; i++) {
        for (j = 2; j <= k; j++) {
            eggFloor[i][j] = INT_MAX;
            for (x = 1; x <= j; x++) {
                res = 1 + max(
                              eggFloor[i - 1][x - 1],
                              eggFloor[i][j - x]);
                if (res < eggFloor[i][j])
                    eggFloor[i][j] = res;
            }
        }
    }
 
    // eggFloor[n][k] holds the result
    return eggFloor[n][k];
}
 
/* Driver program to test to pront printDups*/
int main()
{
    int n = 2, k = 36;
    printf("\nMinimum number of trials "
           "in worst case with %d eggs and "
           "%d floors is %d \n",
           n, k, eggDrop(n, k));
    return 0;
}

Java

// A Dynamic Programming based Java
// Program for the Egg Dropping Puzzle
class EggDrop {
 
    // A utility function to get
    // maximum of two integers
    static int max(int a, int b)
    {
        return (a > b) ? a : b;
    }
 
    /* Function to get minimum number
 of trials needed in worst
    case with n eggs and k floors */
    static int eggDrop(int n, int k)
    {
        /* A 2D table where entery eggFloor[i][j]
 will represent minimum number of trials
needed for i eggs and j floors. */
        int eggFloor[][] = new int[n + 1][k + 1];
        int res;
        int i, j, x;
 
        // We need one trial for one floor and
        // 0 trials for 0 floors
        for (i = 1; i <= n; i++) {
            eggFloor[i][1] = 1;
            eggFloor[i][0] = 0;
        }
 
        // We always need j trials for one egg
        // and j floors.
        for (j = 1; j <= k; j++)
            eggFloor[1][j] = j;
 
        // Fill rest of the entries in table using
        // optimal substructure property
        for (i = 2; i <= n; i++) {
            for (j = 2; j <= k; j++) {
                eggFloor[i][j] = Integer.MAX_VALUE;
                for (x = 1; x <= j; x++) {
                    res = 1 + max(
                                  eggFloor[i - 1][x - 1],
                                  eggFloor[i][j - x]);
                    if (res < eggFloor[i][j])
                        eggFloor[i][j] = res;
                }
            }
        }
 
        // eggFloor[n][k] holds the result
        return eggFloor[n][k];
    }
 
    /* Driver program to test to pront printDups*/
    public static void main(String args[])
    {
        int n = 2, k = 10;
        System.out.println("Minimum number of trials in worst"
                           + " case with "
                           + n + "  eggs and "
                           + k + " floors is " + eggDrop(n, k));
    }
}
/*This code is contributed by Rajat Mishra*/

Python

# A Dynamic Programming based Python Program for the Egg Dropping Puzzle
INT_MAX = 32767
 
# Function to get minimum number of trials needed in worst
# case with n eggs and k floors
def eggDrop(n, k):
    # A 2D table where entery eggFloor[i][j] will represent minimum
    # number of trials needed for i eggs and j floors.
    eggFloor = [[0 for x in range(k + 1)] for x in range(n + 1)]
 
    # We need one trial for one floor and0 trials for 0 floors
    for i in range(1, n + 1):
        eggFloor[i][1] = 1
        eggFloor[i][0] = 0
 
    # We always need j trials for one egg and j floors.
    for j in range(1, k + 1):
        eggFloor[1][j] = j
 
    # Fill rest of the entries in table using optimal substructure
    # property
    for i in range(2, n + 1):
        for j in range(2, k + 1):
            eggFloor[i][j] = INT_MAX
            for x in range(1, j + 1):
                res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x])
                if res < eggFloor[i][j]:
                    eggFloor[i][j] = res
 
    # eggFloor[n][k] holds the result
    return eggFloor[n][k]
 
# Driver program to test to pront printDups
n = 2
k = 36
print("Minimum number of trials in worst case with" + str(n) + "eggs and "
       + str(k) + " floors is " + str(eggDrop(n, k)))
 
# This code is contributed by Bhavya Jain

C#

// A Dynamic Programming based C# Program
// for the Egg Dropping Puzzle
using System;
 
class GFG {
 
    // A utility function to get maximum of
    // two integers
    static int max(int a, int b)
    {
        return (a > b) ? a : b;
    }
 
    /* Function to get minimum number of
    trials needed in worst case with n
    eggs and k floors */
    static int eggDrop(int n, int k)
    {
 
        /* A 2D table where entery eggFloor[i][j]
        will represent minimum number of trials
        needed for i eggs and j floors. */
        int[, ] eggFloor = new int[n + 1, k + 1];
        int res;
        int i, j, x;
 
        // We need one trial for one floor and0
        // trials for 0 floors
        for (i = 1; i <= n; i++) {
            eggFloor[i, 1] = 1;
            eggFloor[i, 0] = 0;
        }
 
        // We always need j trials for one egg
        // and j floors.
        for (j = 1; j <= k; j++)
            eggFloor[1, j] = j;
 
        // Fill rest of the entries in table
        // using optimal substructure property
        for (i = 2; i <= n; i++) {
            for (j = 2; j <= k; j++) {
                eggFloor[i, j] = int.MaxValue;
                for (x = 1; x <= j; x++) {
                    res = 1 + max(eggFloor[i - 1, x - 1],
                                  eggFloor[i, j - x]);
                    if (res < eggFloor[i, j])
                        eggFloor[i, j] = res;
                }
            }
        }
 
        // eggFloor[n][k] holds the result
        return eggFloor[n, k];
    }
 
    // Driver function
    public static void Main()
    {
        int n = 2, k = 36;
        Console.WriteLine("Minimum number of trials "
                          + "in worst case with " + n + " eggs and "
                          + k + "floors is " + eggDrop(n, k));
    }
}
 
// This code is contributed by Sam007.

的PHP


输出
Minimum number of trials in worst case with 2 eggs and 36 floors is 8 

输出 :

Minimum number of trials in worst 
case with 2 eggs and 36 floors is 8

复杂度分析:

  • 时间复杂度: O(n * k ^ 2)。
    其中,“ n”是鸡蛋的数量,“ k”是楼层的数量,因为我们对每个鸡蛋使用嵌套的循环“ k ^ 2”次
  • 辅助空间: O(n * k)。
    由于大小为’n * k’的二维数组用于存储元素。

方法3:使用备注进行动态编程。

C++

#include 
using namespace std;
#define MAX 1000
 
vector> memo(MAX, vector (MAX, -1));
int solveEggDrop(int n, int k) {
  
    if(memo[n][k] != -1) { return memo[n][k];}
     
    if (k == 1 || k == 0)
      return k;
 
    if (n == 1)
      return k;
 
    int min = INT_MAX, x, res;
 
    for (x = 1; x <= k; x++) {
      res = max(
        solveEggDrop(n - 1, x - 1),
        solveEggDrop(n, k - x));
      if (res < min)
        min = res;
    }
     
    memo[n][k] = min+1;
    return min + 1;
  }
 
int main() {
 
    int n = 2, k = 36;
    cout<
输出
8

作为练习,您可以尝试修改上述DP解决方案以打印所有中间楼层(用于最小试验解决方案的楼层)。
更有效的解决方案:鸡蛋掉落之谜(二项式系数和二元搜索解决方案)
2个鸡蛋和K层落鸡蛋拼图
2个鸡蛋和100个地板拼图