-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheggDrop.js
More file actions
44 lines (37 loc) · 877 Bytes
/
eggDrop.js
File metadata and controls
44 lines (37 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script>
/* Function to get minimum number of
trials needed in worst case with n
eggs and k floors */
function eggDrop(n,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;
let min = Number.MAX_VALUE;
let 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
let n = 2, k = 10;
document.write("Minimum number of "
+ "trials in worst case with "
+ n + " eggs and " + k
+ " floors is " + eggDrop(n, k));
</script>