lord_vinayak's blog

By lord_vinayak, history, 2 months ago, In English

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.

Full text and comments »

Tags dp
  • Vote: I like it
  • -1
  • Vote: I do not like it