Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Closest Dessert Cost

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1774" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You would like to make dessert and are preparing to buy the ingredients. You have n ice cream base flavors and m types of toppings to choose from. You must follow these rules when making your dessert: There must be exactly one ice cream base. You can add one or more types of topping or have no toppings at all. There are at most two of each type of topping. You are given three inputs: baseCosts, an integer array of length n, where each baseCosts[i] represents the price of the ith ice cream base flavor. toppingCosts, an integer array of length m, where each toppingCosts[i] is the price of one of the ith topping. target, an integer representing your target price for dessert. You want to make a dessert with a total cost as close to target as possible. Return the closest possible cost of the dessert to target. If there are multiple, return the lower one. Example 1: Input: baseCosts = [1,7], toppingCosts = [3,4], target = 10 Output: 10 Explanation: Consider the following combination (all 0-indexed): - Choose base 1: cost 7 - Take 1 of topping 0: cost 1 x 3 = 3 - Take 0 of topping 1: cost 0 x 4 = 0 Total: 7 + 3 + 0 = 10. Example 2: Input: baseCosts = [2,3], toppingCosts = [4,5,100], target = 18 Output: 17 Explanation: Consider the following combination (all 0-indexed): - Choose base 1: cost 3 - Take 1 of topping 0: cost 1 x 4 = 4 - Take 2 of topping 1: cost 2 x 5 = 10 - Take 0 of topping 2: cost 0 x 100 = 0 Total: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18. Example 3: Input: baseCosts = [3,10], toppingCosts = [2,5], target = 9 Output: 8 Explanation: It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost. Constraints: n == baseCosts.length m == toppingCosts.length 1 <= n, m <= 10 1 <= baseCosts[i], toppingCosts[i] <= 104 1 <= target <= 104

Explanation

  • Start with each base cost: Iterate through each base cost as the starting point for the dessert's total cost.
    • Explore topping combinations recursively: For each base cost, recursively explore all possible combinations of toppings, considering 0, 1, or 2 of each topping type.
    • Track the closest cost: Maintain a variable to track the closest cost to the target found so far. Update this variable whenever a closer cost is encountered during the recursive exploration.
  • Runtime Complexity: O(n * 3m), where n is the number of base costs and m is the number of topping costs.
  • Storage Complexity: O(m), due to the recursive call stack.

Code

    def closestCost(baseCosts, toppingCosts, target):
    closest = float('inf')

    def calculate_cost(current_cost, topping_index):
        nonlocal closest

        if abs(current_cost - target) < abs(closest - target):
            closest = current_cost
        elif abs(current_cost - target) == abs(closest - target):
            closest = min(closest, current_cost)

        if topping_index == len(toppingCosts):
            return

        # Option 1: Take zero of this topping
        calculate_cost(current_cost, topping_index + 1)

        # Option 2: Take one of this topping
        calculate_cost(current_cost + toppingCosts[topping_index], topping_index + 1)

        # Option 3: Take two of this topping
        calculate_cost(current_cost + 2 * toppingCosts[topping_index], topping_index + 1)

    for base_cost in baseCosts:
        calculate_cost(base_cost, 0)

    return closest

More from this blog

C

Chatmagic blog

2894 posts