File tree 1 file changed +26
-0
lines changed
solution/1700-1799/1774.Closest Dessert Cost
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -277,6 +277,32 @@ func abs(x int) int {
277
277
}
278
278
```
279
279
280
+ ### ** JavaScript**
281
+
282
+ ``` js
283
+ const closestCost = function (baseCosts , toppingCosts , target ) {
284
+ let closestDessertCost = - Infinity ;
285
+ function dfs (dessertCost , j ) {
286
+ const tarCurrDiff = Math .abs (target - dessertCost);
287
+ const tarCloseDiff = Math .abs (target - closestDessertCost);
288
+ if (tarCurrDiff < tarCloseDiff) {
289
+ closestDessertCost = dessertCost;
290
+ } else if (tarCurrDiff === tarCloseDiff && dessertCost < closestDessertCost) {
291
+ closestDessertCost = dessertCost;
292
+ }
293
+ if (dessertCost > target) return ;
294
+ if (j === toppingCosts .length ) return ;
295
+ for (let count = 0 ; count <= 2 ; count++ ) {
296
+ dfs (dessertCost + count * toppingCosts[j], j + 1 );
297
+ }
298
+ }
299
+ for (let i = 0 ; i < baseCosts .length ; i++ ) {
300
+ dfs (baseCosts[i], 0 );
301
+ }
302
+ return closestDessertCost;
303
+ };
304
+ ```
305
+
280
306
### ** ...**
281
307
282
308
```
You can’t perform that action at this time.
0 commit comments