Leetcode #2870. Minimum Number of Operations to Make Array Empty

ยท

1 min read

https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/?envType=daily-question&envId=2024-01-04

We first iterate calculate the count of each element and store it in an object.

If any of the count is 1, we return -1 because the there no enough element to make the array empty.

We then add divide each count by 3, ceil the value incase we get a decimal, add them up and return the sum.

There is more reason why this works but for now, this is all I have got.

var minOperations = function (nums) {

    let counter = {}, result = 0

    for (let num of nums) {
        counter[num] = (counter[num] || 0) + 1
    }

    for (let [num, count] of Object.entries(counter)) {
        if (count === 1) return -1

        result += Math.ceil(count / 3)
    }

    return result

};
ย