Skip to content

Commit 7a5fa39

Browse files
feat: add js solution to lc problem: No.1774 (doocs#2219)
1 parent d934a3d commit 7a5fa39

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: solution/1700-1799/1774.Closest Dessert Cost/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,32 @@ func abs(x int) int {
238238
}
239239
```
240240

241+
### **JavaScript**
242+
243+
```js
244+
const closestCost = function (baseCosts, toppingCosts, target) {
245+
let closestDessertCost = -Infinity;
246+
function dfs(dessertCost, j) {
247+
const tarCurrDiff = Math.abs(target - dessertCost);
248+
const tarCloseDiff = Math.abs(target - closestDessertCost);
249+
if (tarCurrDiff < tarCloseDiff) {
250+
closestDessertCost = dessertCost;
251+
} else if (tarCurrDiff === tarCloseDiff && dessertCost < closestDessertCost) {
252+
closestDessertCost = dessertCost;
253+
}
254+
if (dessertCost > target) return;
255+
if (j === toppingCosts.length) return;
256+
for (let count = 0; count <= 2; count++) {
257+
dfs(dessertCost + count * toppingCosts[j], j + 1);
258+
}
259+
}
260+
for (let i = 0; i < baseCosts.length; i++) {
261+
dfs(baseCosts[i], 0);
262+
}
263+
return closestDessertCost;
264+
};
265+
```
266+
241267
### **...**
242268

243269
```

0 commit comments

Comments
 (0)