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 @@ -238,6 +238,32 @@ func abs(x int) int {
238
238
}
239
239
```
240
240
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
+
241
267
### ** ...**
242
268
243
269
```
You can’t perform that action at this time.
0 commit comments