Here’s my code for exchanging coins and it gives tle. Can you tell me where am I going wrong? https://ide.codingblocks.com/s/358914
Exchanging coins
Hey @div_yanshu07
Why u are mixing top down and bottom up approach.
Stick to top down on this because of constraints n <10^9 we cant use do array of that size
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> mp;
int solve(int n){
if(n==0)return 0;
if(n==1)return 1;
if(mp[n])return mp[n];
return mp[n]=max(n,solve(n/2)+solve(n/3)+solve(n/4));
}
int main() {
int n;
cin>>n;
cout<<solve(n);
return 0;
}
Here see this for reference
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.