File tree 1 file changed +45
-0
lines changed
1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include<bits/stdc++.h>
2
+ using namespace std;
3
+
4
+ // A utility function to get maximum of two integers
5
+ int max(int a, int b) { return (a > b)? a: b; }
6
+
7
+ /* Function to get minimum number of trials needed in worst
8
+ case with n eggs and k floors */
9
+ int eggDrop(int n, int k)
10
+ {
11
+ // If there are no floors, then no trials needed. OR if there is
12
+ // one floor, one trial needed.
13
+ if (k == 1 || k == 0)
14
+ return k;
15
+
16
+ // We need k trials for one egg and k floors
17
+ if (n == 1)
18
+ return k;
19
+
20
+ int min = INT_MAX, x, res;
21
+
22
+ // Consider all droppings from 1st floor to kth floor and
23
+ // return the minimum of these values plus 1.
24
+ for (x = 1; x <= k; x++)
25
+ {
26
+ res = max(eggDrop(n-1, x-1), eggDrop(n, k-x));
27
+ if (res < min)
28
+ min = res;
29
+ }
30
+
31
+ return min + 1;
32
+ }
33
+
34
+ /* Driver program to test to pront printDups*/
35
+ int main()
36
+ {
37
+ int n = 2, k = 10;
38
+ cout << "Minimum number of trials in worst case with "
39
+ << n << " eggs and " << k << " floors is "
40
+ << eggDrop(n, k) << endl;
41
+ return 0;
42
+ }
43
+
44
+ // This code is contributed
45
+ // by Akanksha Rai
You can’t perform that action at this time.
0 commit comments