Minimum Cost of Buying Candies With Discount LeetCode solution in JavaScript

1
var minimumCost = function (cost) {
2
if (cost.length < 3) {
3
return cost.reduce((prev, cur) => prev + cur);
4
}
5

6
cost.sort((a, b) => b - a);
7
let count = 0;
8
let sum = 0;
9

10
for (const num of cost) {
11
if (count === 2) {
12
count = 0;
13
continue;
14
}
15
sum += num;
16
count++;
17
}
18

19
return sum;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0