forked from Nivedpv2004/C-PROGRAMING
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eggDroppingProblem.cpp
50 lines (48 loc) · 1.28 KB
/
eggDroppingProblem.cpp
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
45
46
47
48
49
50
// DP - EggDropping Memoization
#include<bits/stdc++.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
using namespace std;
int static dp[1001][1001];
int EggDrop(int egg,int floor)
{
int dp[egg+1][floor+1];
memset(dp,-1,sizeof(dp));
/*if(egg==0)
return 0;*/
if(floor==0 || floor==1)
return floor;
if(egg==1)
return floor; // worst case scenario
if(dp[egg][floor]!=-1)
return dp[egg][floor];
int ans=INT_MAX;
for(int k=1;k<=floor;k++)
{
int low,high;
if(dp[egg-1][k-1]!=-1)
low=dp[egg-1][k-1];
else{
low=EggDrop(egg-1,k-1);
dp[egg-1][k-1]=low;
}
if(dp[egg][floor-k]!=-1)
high=dp[egg][floor-k];
else{
high=EggDrop(egg,floor-k);
dp[egg][floor-k]=high;
}
int tempans=1+max(low,high);
ans=min(ans,tempans);
}
return dp[egg][floor]=ans;
}
int main()
{
int egg,floor;
cout<<"Total no of eggs and floor : ";
cin>>egg>>floor;
cout<<"\n Minimum no of attempts in worst case to find critical floor : "<<EggDrop(egg,floor)<<endl;
return 0;
}