Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. int minimumCost(coupon_t coupons[], int numCoupons, int units){
  2.    
  3.     int i;
  4.     int cost= -1; temp;
  5.     if (!units) return 0; //base case when you are able to buy all units by using all coupons
  6.  
  7.     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
  8.                     //used
  9.         if( units>=coupons[i].quantity &&
  10.         (temp = minimumCost(coupons, i, units - coupons[i].quantity)>=0 )
  11.         //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
  12.             temp += coupons[i].price; //need to add on coupons[i].price since we are using it;
  13.        
  14.         if(cost = -1 || (temp < cost && temp!=-1))
  15.             cost = temp;
  16.     }
  17.    
  18.     return cost;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement