Skip to content

Commit

Permalink
Added Egg Drop (jainaman224#1013)
Browse files Browse the repository at this point in the history
*  Egg Drop

* Update Egg_Drop.c

* Update Egg_Drop.cpp
  • Loading branch information
somya-kapoor authored and sridharjajoo committed Apr 19, 2019
1 parent 02e4b26 commit ee0315c
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Egg_Drop/Egg_Drop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# include <stdio.h>

// To get maximum of two integers
#define max(a, b) ((a > b)? a : b)

int eggDrop(int eggs, int floors)
{
/* A 2D table where entery eggFloor[i][j] will represent minimum
number of trials needed for e eggs and f floors. */
int eggfloor[eggs][floors];
for (int e = 1; e <= eggs; e++)
{
for (int f = 0; f <= floors; f++)
{
if(f == 0)
{
eggfloor[e][f] = 0;
}
else if(e == 1)
{
eggfloor[e][f] = f;
}
else
{
// Fill rest of the entries in table using optimal substructure
// property
int min = 9999;
for (int i = 1; i <= f; i++)
{
int sol = max(eggfloor[e-1][i-1], eggfloor[e][f-i]) + 1;
if(sol < min)
{
min = sol;
}
}
eggfloor[e][f] = min;
}
}
}
return eggfloor[eggs][floors];
}

int main()
{
int eggs = 2, floors = 10;
printf("The number of eggs: %d\n", eggs);
printf("The number of floors: %d\n", floors);
printf("Minimum number of trials: %d ", eggDrop(eggs, floors));
return 0;
}

/*The number of eggs: 2
The number of floors: 10
Minimum number of trials: 4*/
59 changes: 59 additions & 0 deletions Egg_Drop/Egg_Drop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# include <stdio.h>
# include <iostream>
using namespace std;

// To get maximum of two integers
#define max(a, b) ((a > b)? a : b)

int eggDrop(int eggs, int floors)
{
/* A 2D table where entery eggFloor[i][j] will represent minimum
number of trials needed for e eggs and f floors. */
int eggfloor[eggs][floors];
for (int e = 1; e <= eggs; e++)
{
for (int f = 0; f <= floors; f++)
{
if(f == 0)
{
eggfloor[e][f] = 0;
}
else if(e == 1)
{
eggfloor[e][f] = f;
}
else
{
// Fill rest of the entries in table using optimal substructure
// property
int min = INT_MAX;
for (int i = 1; i <= f; i++)
{
int sol = max(eggfloor[e-1][i-1], eggfloor[e][f-i]) + 1;
if(sol < min)
{
min = sol;
}
}
eggfloor[e][f] = min;
}
}
}
return eggfloor[eggs][floors];
}

int main()
{
int eggs ,floors;
cout << "Enter the number of eggs: ";
cin >> eggs;
cout << "Enter the number of floors: ";
cin >> floors;

cout << "Minimum number of trials: " << eggDrop(eggs, floors) << endl;
return 0;
}

/*Enter the number of eggs: 2
Enter the number of floors: 10
Minimum number of trials: 4*/

0 comments on commit ee0315c

Please sign in to comment.