forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Egg Drop * Update Egg_Drop.c * Update Egg_Drop.cpp
- Loading branch information
1 parent
02e4b26
commit ee0315c
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*/ |