Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find closest dessert cost in Python
In Python, we can solve the closest dessert cost problem by systematically exploring all possible combinations of bases and toppings. Given arrays of baseCosts and toppingCosts with a target value, we need to find the dessert cost closest to the target.
Problem Rules
There must be exactly one base
We can add zero, one, or more toppings
At most two of each type of topping can be used
If multiple costs are equally close to target, return the lower one
Algorithm Approach
We use a bitmask approach to represent topping combinations. Each position in the bitmask represents how many times (0, 1, or 2) we use each topping type ?
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
result = solve(baseCosts, toppingCosts, target)
print(f"Closest dessert cost: {result}")
Closest dessert cost: 12
How It Works
The algorithm works by:
Base Selection: Try each base cost from the baseCosts array
Topping Combinations: Use a bitmask to generate all possible topping combinations (0, 1, or 2 of each type)
Cost Calculation: For each combination, calculate total cost = base + sum of toppings
Best Cost Update: Keep track of the cost closest to target, preferring lower costs when distances are equal
Example Walkthrough
With baseCosts = [2, 8], toppingCosts = [4, 5], and target = 12 ?
def solve_with_details(baseCosts, toppingCosts, target):
best_cost = baseCosts[0]
print(f"Initial best_cost: {best_cost}")
for base_idx, base_cost in enumerate(baseCosts):
print(f"\nTrying base {base_idx}: cost = {base_cost}")
# Try some key combinations
combinations = [
[0, 0], # No toppings
[1, 0], # 1 of first topping
[0, 1], # 1 of second topping
[1, 1], # 1 of each topping
]
for combo in combinations:
total_cost = base_cost
for i, count in enumerate(combo):
total_cost += count * toppingCosts[i]
distance = abs(total_cost - target)
print(f" Toppings {combo}: cost = {total_cost}, distance = {distance}")
if distance < abs(best_cost - target):
best_cost = total_cost
print(f" New best_cost: {best_cost}")
return best_cost
baseCosts = [2, 8]
toppingCosts = [4, 5]
target = 12
result = solve_with_details(baseCosts, toppingCosts, target)
print(f"\nFinal result: {result}")
Initial best_cost: 2
Trying base 0: cost = 2
Toppings [0, 0]: cost = 2, distance = 10
Toppings [1, 0]: cost = 6, distance = 6
New best_cost: 6
Toppings [0, 1]: cost = 7, distance = 5
New best_cost: 7
Toppings [1, 1]: cost = 11, distance = 1
New best_cost: 11
Trying base 1: cost = 8
Toppings [0, 0]: cost = 8, distance = 4
Toppings [1, 0]: cost = 12, distance = 0
New best_cost: 12
Toppings [0, 1]: cost = 13, distance = 1
Toppings [1, 1]: cost = 17, distance = 5
Final result: 12
Conclusion
The algorithm systematically explores all valid dessert combinations using a bitmask approach. It finds the cost closest to the target by trying each base with all possible topping combinations, ensuring we get the optimal solution.
