Program to find closest dessert cost in Python


Suppose we have two arrays called baseCosts with n items from them we can select base and toppingCosts with m items from them we can select toppings and also have a target value. We have to follow these rules to make dessert.

  • There must be exactly one base.

  • We can add one or more topping or have no toppings at all.

  • There are at most two of each type of topping.

Here baseCosts[i] represents the price of the ith ice cream base. The toppingCosts[i] represents price of one of the ith topping. The target value is representing the target price for dessert. We have to make a dessert with a total cost as close to target as possible. We have to find the closest possible cost of the dessert to target. If there are multiple answers, return the lower one.

So, if the input is like baseCosts = [2,8], toppingCosts = [4,5], target = 12, then the output will be 12 because we can take base with cost 8, then take 1 of first topping with cost 4, and no type2 topping, so total cost 8+4 = 12.

To solve this, we will follow these steps −

  • best_cost := baseCosts[0]

  • for b in range 0 to size of baseCosts - 1, do

    • bitmask := an array whose size is same as size of toppingCosts and fill with 0

    • do the following infinitely, do

      • current_price := baseCosts[b]

      • for j in range 0 to size of bitmask - 1, do

        • current_price := current_price + bitmask[j] * toppingCosts[j]

      • if current_price - target is same as 0, then

        • return target

      • otherwise when |current_price - target| < |best_cost - target|, then

        • best_cost := current_price

      • otherwise when |current_price - target| is same as |best_cost - target|, then

        • if current_price < best_cost, then

          • best_cost := current_price

      • if 0 not in bitmask and 1 not in bitmask, then

        • come out from loop

      • for i in range 0 to size of bitmask - 1, do

        • if bitmask[i] is not same as 2, then

          • bitmask[i] := bitmask[i] + 1

          • come out from loop

        • otherwise,

          • bitmask[i] := 0

  • return best_cost

Example

Let us see the following implementation to get better understanding −

def solve(baseCosts, toppingCosts, target):
   best_cost = baseCosts[0]

   for b in range(len(baseCosts)):
      bitmask = [0] * len(toppingCosts)
      while True:
         current_price = baseCosts[b]
         for j in range(len(bitmask)):
            current_price += bitmask[j] * toppingCosts[j]
         if current_price - target == 0:
            return target
         elif abs(current_price - target) < abs(best_cost - target):
            best_cost = current_price
         elif abs(current_price - target) == abs(best_cost - target):
            if current_price < best_cost:
               best_cost = current_price

         if 0 not in bitmask and 1 not in bitmask:
            break
         for i in range(len(bitmask)):
            if bitmask[i] != 2:
               bitmask[i] += 1
               break
            else:
               bitmask[i] = 0
   return best_cost

baseCosts = [2,8]
toppingCosts = [4,5]
target = 12
print(solve(baseCosts, toppingCosts, target))

Input

[2,8], [4,5], 12

Output

12

Updated on: 06-Oct-2021

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements