change(rs) gives the coins required for remaining value
- change(0) = 0 // we need 0 coins to produce 0 cents
- change(< 0) = ∞ // in practice, we can return a large positive value
- change(value) = 1 + min(change(value — coinValue[i])) ∀ i ∈ [0..n-1]
I wrote this but its not working (gives a large integer value !) ~~~~~
define INT_MA 999999
int memo[1000],v[1000]; int coin(int rs) { if(rs == 0) return 0; if(rs<0) return INT_MA; if(memo[rs] != INT_MA) return memo[rs]; int &a = memo[rs]; for(int i=0;i<n;i++) { a=min(a,coin(rs-v[i])); } if(a!=INT_MA) a=a+1; return a; } int main() { int n,val; cin>>n>>val;
for(int i=0;i<n;i++) cin>>v[i];
memset(memo,INT_MA,sizeof memo); cout<<coin(val); }
~~~~~