voiddfs(constint *toppingCosts, int toppingCostsSize, int p, int curCost, int *res, constint target) { if (abs(*res - target) < curCost - target) { return; } elseif (abs(*res - target) >= abs(curCost - target)) { if (abs(*res - target) > abs(curCost - target)) { *res = curCost; } else { *res = MIN(*res, curCost); } } if (p == toppingCostsSize) { return; } dfs(toppingCosts, toppingCostsSize, p + 1, curCost + toppingCosts[p] * 2, res, target); dfs(toppingCosts, toppingCostsSize, p + 1, curCost + toppingCosts[p], res, target); dfs(toppingCosts, toppingCostsSize, p + 1, curCost, res, target); }
intclosestCost(int* baseCosts, int baseCostsSize, int* toppingCosts, int toppingCostsSize, int target) { int res = INT_MAX; for (int i = 0; i < baseCostsSize; i++) { res = MIN(res, baseCosts[i]); } for (int i = 0; i < baseCostsSize; i++) { dfs(toppingCosts, toppingCostsSize, 0, baseCosts[i], &res, target); } return res; }
funcclosestCost(baseCosts []int, toppingCosts []int, target int)int { ans := baseCosts[0] for _, c := range baseCosts { ans = min(ans, c) } var dfs func(int, int) dfs = func(p, curCost int) { if abs(ans-target) < curCost-target { return } elseif abs(ans-target) >= abs(curCost-target) { if abs(ans-target) > abs(curCost-target) { ans = curCost } else { ans = min(ans, curCost) } } if p == len(toppingCosts) { return } dfs(p+1, curCost+toppingCosts[p]*2) dfs(p+1, curCost+toppingCosts[p]) dfs(p+1, curCost) } for _, c := range baseCosts { dfs(0, c) } return ans }
funcabs(x int)int { if x < 0 { return -x } return x }
funcmin(a, b int)int { if a > b { return b } return a }
classSolution: defclosestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int: x = min(baseCosts) if x > target: return x can = [False] * (target + 1) ans = 2 * target - x for c in baseCosts: if c <= target: can[c] = True else: ans = min(ans, c) for c in toppingCosts: for count inrange(2): for i inrange(target, 0, -1): if can[i] and i + c > target: ans = min(ans, i + c) if i - c > 0andnot can[i]: can[i] = can[i - c] for i inrange(ans - target + 1): if can[target - i]: return target - i return ans
publicclassSolution { publicintClosestCost(int[] baseCosts, int[] toppingCosts, int target) { int x = baseCosts.Min(); if (x >= target) { return x; } bool[] can = newbool[target + 1]; int res = 2 * target - x; foreach (int b in baseCosts) { if (b <= target) { can[b] = true; } else { res = Math.Min(res, b); } } foreach (int t in toppingCosts) { for (int count = 0; count < 2; ++count) { for (int i = target; i > 0; --i) { if (can[i] && i + t > target) { res = Math.Min(res, i + t); } if (i - t > 0) { can[i] = can[i] | can[i - t]; } } } } for (int i = 0; i <= res - target; ++i) { if (can[target - i]) { return target - i; } } return res; } }
var closestCost = function(baseCosts, toppingCosts, target) { const x = _.min(baseCosts); if (x >= target) { return x; } const can = newArray(target + 1).fill(0); let res = 2 * target - x; for (const b of baseCosts) { if (b <= target) { can[b] = true; } else { res = Math.min(res, b); } } for (const t of toppingCosts) { for (let count = 0; count < 2; ++count) { for (let i = target; i > 0; --i) { if (can[i] && i + t > target) { res = Math.min(res, i + t); } if (i - t > 0) { can[i] = can[i] | can[i - t]; } } } } for (let i = 0; i <= res - target; ++i) { if (can[target - i]) { return target - i; } } return res; }
funcclosestCost(baseCosts []int, toppingCosts []int, target int)int { x := baseCosts[0] for _, c := range baseCosts { x = min(x, c) } if x > target { return x } can := make([]bool, target+1) ans := 2*target - x for _, c := range baseCosts { if c <= target { can[c] = true } else { ans = min(ans, c) } } for _, c := range toppingCosts { for count := 0; count < 2; count++ { for i := target; i > 0; i-- { if can[i] && i+c > target { ans = min(ans, i+c) } if i-c > 0 { can[i] = can[i] || can[i-c] } } } } for i := 0; i <= ans-target; i++ { if can[target-i] { return target - i } } return ans }
funcmin(a, b int)int { if a > b { return b } return a }