Skip to content

Commit

Permalink
Leetcode/2870
Browse files Browse the repository at this point in the history
  • Loading branch information
herrera-ignacio committed Jan 4, 2024
1 parent cdc2ca1 commit 8fa0983
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 23 additions & 0 deletions problems/leetcode/2870/solution.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8fa0983

Please sign in to comment.