Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int minimumCost(coupon_t coupons[], int numCoupons, int units){
- int i;
- int cost= -1; temp;
- if (!units) return 0; //base case when you are able to buy all units by using all coupons
- for(i=numCoupons-1; i>-1; i++){ //we count the minimum for each of the'i'-th coupon that is the largest indexed coupon to be
- //used
- if( units>=coupons[i].quantity &&
- (temp = minimumCost(coupons, i, units - coupons[i].quantity)>=0 )
- //recursion using i because we now consider the first i-1 tickets; we assumed ith coupon is largest indexed coupon //would be used. Also if temp = -1, we skip since no viable solution
- temp += coupons[i].price; //need to add on coupons[i].price since we are using it;
- if(cost = -1 || (temp < cost && temp!=-1))
- cost = temp;
- }
- return cost;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement