DP : Minimum no. of coins needed to make given amount — for large values

Revision en1, by lord_vinayak, 2024-12-14 18:53:48

I am trying to attempt 996A - Hit the Lottery and have written its solution using memoization. The code works for smaller values but it fails to execute for large inputs. What changes I need to make to get the problem accepted?

const int N = 1e5;
int dp[N]; int coins[5]={1,5,10,20,100};
int func(int amt){
	if(amt==0) return 0;
	int ans = INT_MAX;
	if(dp[amt]!=-1) return dp[amt];
	for(int coin:coins){
		if(amt-coin>=0)
			ans = min(ans, func(amt-coin)+1);
	}
	return dp[amt]=ans;
}
int main()
{
	FAST_IO
	int amt; cin>>amt;
	memset(dp, -1, sizeof(dp));
	
	int ans = func(amt);
	if(ans==INT_MAX) cout<<"-1";
	else cout<<ans;

}

I tried changing ans = min(ans, func(amt-coin)+1) to ans = min(ans+0LL, func(amt-coin)+1LL) but it still doesn't show any output. Help me out with changes I need to do.

Tags dp

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English lord_vinayak 2024-12-14 18:53:48 908 Initial revision (published)