|
| 1 | +# Time: O(m * max(max_base, target + max_topping / 2)) ~= O(m * t) |
| 2 | +# Space: O(max(max_base, target + max_topping / 2)) ~= O(t) |
| 3 | + |
| 4 | +class Solution(object): |
| 5 | + def closestCost(self, baseCosts, toppingCosts, target): |
| 6 | + """ |
| 7 | + :type baseCosts: List[int] |
| 8 | + :type toppingCosts: List[int] |
| 9 | + :type target: int |
| 10 | + :rtype: int |
| 11 | + """ |
| 12 | + max_count = 2 |
| 13 | + max_base, max_topping = max(baseCosts), max(toppingCosts) |
| 14 | + dp = [False]*(max(max_base, target+max_topping//2)+1) |
| 15 | + for b in baseCosts: |
| 16 | + dp[b] = True |
| 17 | + 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] = True |
| 22 | + result = float("inf") |
| 23 | + for i in xrange(1, len(dp)): |
| 24 | + if not dp[i]: |
| 25 | + continue |
| 26 | + if abs(i-target) < abs(result-target): |
| 27 | + result = i |
| 28 | + if i >= target: |
| 29 | + break |
| 30 | + return result |
| 31 | + |
| 32 | + |
| 33 | +# Time: O(n * 3^m) |
| 34 | +# Space: O(m * t) |
| 35 | +class Solution2(object): |
| 36 | + def closestCost(self, baseCosts, toppingCosts, target): |
| 37 | + """ |
| 38 | + :type baseCosts: List[int] |
| 39 | + :type toppingCosts: List[int] |
| 40 | + :type target: int |
| 41 | + :rtype: int |
| 42 | + """ |
| 43 | + max_count = 2 |
| 44 | + def backtracking(toppingCosts, i, cost, target, lookup, result): |
| 45 | + if (i, cost) in lookup: |
| 46 | + return |
| 47 | + 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] = cost |
| 51 | + return |
| 52 | + 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 | + |
| 62 | +# Time: O(3^m*log(3^m)) + O(n*log(3^m)) = O(m*(3^m + n)) |
| 63 | +# Space: O(3^m) |
| 64 | +import bisect |
| 65 | + |
| 66 | + |
| 67 | +class Solution3(object): |
| 68 | + def closestCost(self, baseCosts, toppingCosts, target): |
| 69 | + """ |
| 70 | + :type baseCosts: List[int] |
| 71 | + :type toppingCosts: List[int] |
| 72 | + :type target: int |
| 73 | + :rtype: int |
| 74 | + """ |
| 75 | + max_count = 2 |
| 76 | + 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 result |
| 87 | + |
| 88 | + |
| 89 | +# Time: O(n * 3^m) |
| 90 | +# Space: O(3^m) |
| 91 | +class Solution4(object): |
| 92 | + def closestCost(self, baseCosts, toppingCosts, target): |
| 93 | + """ |
| 94 | + :type baseCosts: List[int] |
| 95 | + :type toppingCosts: List[int] |
| 96 | + :type target: int |
| 97 | + :rtype: int |
| 98 | + """ |
| 99 | + max_count = 2 |
| 100 | + 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 result |
0 commit comments