diff --git a/C++ Codes/egg_droping_optimized_code.cpp b/C++ Codes/egg_droping_optimized_code.cpp new file mode 100644 index 0000000..6dc3b53 --- /dev/null +++ b/C++ Codes/egg_droping_optimized_code.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; +int dp[20][100]; +int solve(int e, int f){ + + if(e==1){return f;} + if(f==0 || f==1){ + return f; + } + if(dp[e][f]!=-1){ + return dp[e][f];} + + int mini =INT_MAX; + int temp=0; + int low,high; + + + + for(int k=1;k<=f;k++){ + + if(dp[e-1][k-1]!=-1){ + low = dp[e-1][k-1]; } + else{ + low = solve(e-1,k-1); + dp[e-1][k-1]=low;} + + if(dp[e][f-k]!=-1){ + high = dp[e][f-k]; } + else{ + high = solve(e,f-k); + dp[e][f-k]=high;} + + temp =1+max(low,high); + mini = min(mini,temp); + } + + return dp[e][f]=mini; + +} + +int main(){ + +int e,f; +memset(dp,-1,sizeof(dp)); +cin>>e>>f; +cout< +using namespace std; +unordered_map mp; +int solve(string s,int i,int j,bool isTrue){ + int ans=0; + if(i==j){ + if(isTrue==1){ + return s[i]=='T';} + else + return s[i]=='F'; + } + if(i>j){return false;} + + string temp= to_string(i); + temp.push_back(' '); + temp.append(to_string(j)); + temp.push_back(' '); + temp.append(to_string(isTrue)); + + if(mp.find(temp)!=mp.end()){ + return mp[temp]; + } + + for(int k=i+1;k<=j-1;k=k+2){ + int LT= solve(s,i,k-1,1); + int RF= solve(s,k+1,j,0); + int LF= solve(s,i,k-1,0); + int RT= solve(s,k+1,j,1); + + if(s[k]=='|'){ + if(isTrue==1){ + ans=ans + LT*RF + RT*LF + LT*RT; + } + else + ans=ans+LF*RF; + } + else if(s[k]=='&'){ + if(isTrue==1){ + ans= ans + LT*RT; + } + else + ans = ans + LT*RF + LF*RT + LF*RF; + } + else if(s[k]=='^'){ + if(isTrue==1){ + ans = ans + LT*RF + LF*RT; + } + else + ans = ans +LT*RT + LF*RF; + } + } + return mp[temp]=ans; + + +} +int main(){ + mp.clear(); + string s; + cin>>s; + int n =s.length(); + cout<