Approach
Depth-first search
For Closest Dessert Cost, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 108 lines of Python from the credited upstream file closest-dessert-cost.py.
- The implementation visibly relies on sequence storage, ordered lookup, cached states.
- No explicit loop blocks detected.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 4class Solution(object):5 def closestCost(self, baseCosts, toppingCosts, target):6 """7 :type baseCosts: List[int]8 :type toppingCosts: List[int]9 :type target: int10 :rtype: int11 """12 max_count = 213 max_base, max_topping = max(baseCosts), max(toppingCosts)14 dp = [False]*(max(max_base, target+max_topping2)+1)15 for b in baseCosts:16 dp[b] = True17 for t in toppingCosts:18 for _ in xrange(max_count):19 for i in reversed(xrange(len(dp)-t)):20 if dp[i]:21 dp[i+t] = True22 result = float("inf")23 for i in xrange(1, len(dp)):24 if not dp[i]:25 continue26 if abs(i-target) < abs(result-target):27 result = i28 if i >= target:29 break30 return result31 32 333435class Solution2(object):36 def closestCost(self, baseCosts, toppingCosts, target):37 """38 :type baseCosts: List[int]39 :type toppingCosts: List[int]40 :type target: int41 :rtype: int42 """43 max_count = 244 def backtracking(toppingCosts, i, cost, target, lookup, result):45 if (i, cost) in lookup:46 return47 lookup.add((i, cost))48 if cost >= target or i == len(toppingCosts):49 if (abs(cost-target), cost) < (abs(result[0]-target), result[0]):50 result[0] = cost51 return52 for j in xrange(max_count+1):53 backtracking(toppingCosts, i+1, cost+j*toppingCosts[i], target, lookup, result)54 55 result = [float("inf")]56 lookup = set()57 for b in baseCosts:58 backtracking(toppingCosts, 0, b, target, lookup, result)59 return result[0]60 61 626364import bisect65 66 67class Solution3(object):68 def closestCost(self, baseCosts, toppingCosts, target):69 """70 :type baseCosts: List[int]71 :type toppingCosts: List[int]72 :type target: int73 :rtype: int74 """75 max_count = 276 combs = set([0])77 for t in toppingCosts:78 combs = set([c+i*t for c in combs for i in xrange(max_count+1)])79 result, combs = float("inf"), sorted(combs)80 for b in baseCosts:81 idx = bisect.bisect_left(combs, target-b)82 if idx < len(combs):83 result = min(result, b+combs[idx], key=lambda x: (abs(x-target), x))84 if idx > 0:85 result = min(result, b+combs[idx-1], key=lambda x: (abs(x-target), x)) 86 return result87 88 899091class Solution4(object):92 def closestCost(self, baseCosts, toppingCosts, target):93 """94 :type baseCosts: List[int]95 :type toppingCosts: List[int]96 :type target: int97 :rtype: int98 """99 max_count = 2100 combs = set([0])101 for t in toppingCosts:102 combs = set([c+i*t for c in combs for i in xrange(max_count+1)])103 result = float("inf")104 for b in baseCosts:105 for c in combs:106 result = min(result, b+c, key=lambda x: (abs(x-target), x)) 107 return result108