diff --git a/README.md b/README.md index fcf42ed..4944111 100644 --- a/README.md +++ b/README.md @@ -231,3 +231,4 @@ - [2131: Longest palindrome by concatenating two letter words](problems/leetcode/2131) - [2225: Find players with zero or one losses](problems/leetcode/2225) - Map - [2610: Convert an array into a 2d array with conditions](problems/leetcode/2610) - Medium - Array +- [2870: Minimum number of operations to make array empty](problems/leetcode/2870) diff --git a/problems/leetcode/2870/solution.js b/problems/leetcode/2870/solution.js new file mode 100644 index 0000000..c297cfc --- /dev/null +++ b/problems/leetcode/2870/solution.js @@ -0,0 +1,23 @@ +/** + * + * @param {number[]} nums + * @return {number} + */ +const minOperations = (nums) => { + const repsByNum = new Map(); + + for (const num of nums) { + repsByNum.set(num, (repsByNum.get(num) || 0) + 1); + } + + let steps = 0; + + for (const reps of repsByNum.values()) { + if (reps === 1) { + return -1; + } + steps += Math.ceil(reps / 3); + } + + return steps; +}