返回介绍

solution / 2800-2899 / 2870.Minimum Number of Operations to Make Array Empty / README_EN

发布于 2024-06-17 01:02:59 字数 4704 浏览 0 评论 0 收藏 0

2870. Minimum Number of Operations to Make Array Empty

中文文档

Description

You are given a 0-indexed array nums consisting of positive integers.

There are two types of operations that you can apply on the array any number of times:

  • Choose two elements with equal values and delete them from the array.
  • Choose three elements with equal values and delete them from the array.

Return _the minimum number of operations required to make the array empty, or _-1_ if it is not possible_.

 

Example 1:

Input: nums = [2,3,3,2,2,4,2,3,4]
Output: 4
Explanation: We can apply the following operations to make the array empty:
- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
It can be shown that we cannot make the array empty in less than 4 operations.

Example 2:

Input: nums = [2,1,2,2,3,3]
Output: -1
Explanation: It is impossible to empty the array.

 

Constraints:

  • 2 <= nums.length <= 105
  • 1 <= nums[i] <= 106

Solutions

Solution 1: Hash Table + Greedy

We use a hash table $count$ to count the number of occurrences of each element in the array. Then we traverse the hash table. For each element $x$, if it appears $c$ times, we can perform $\lfloor \frac{c+2}{3} \rfloor$ operations to delete $x$. Finally, we return the sum of the number of operations for all elements.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(n)$.

class Solution:
  def minOperations(self, nums: List[int]) -> int:
    count = Counter(nums)
    ans = 0
    for c in count.values():
      if c == 1:
        return -1
      ans += (c + 2) // 3
    return ans
class Solution {
  public int minOperations(int[] nums) {
    Map<Integer, Integer> count = new HashMap<>();
    for (int num : nums) {
      // count.put(num, count.getOrDefault(num, 0) + 1);
      count.merge(num, 1, Integer::sum);
    }
    int ans = 0;
    for (int c : count.values()) {
      if (c < 2) {
        return -1;
      }
      int r = c % 3;
      int d = c / 3;
      switch (r) {
        case (0) -> {
          ans += d;
        }
        default -> {
          ans += d + 1;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minOperations(vector<int>& nums) {
    unordered_map<int, int> count;
    for (int num : nums) {
      ++count[num];
    }
    int ans = 0;
    for (auto& [_, c] : count) {
      if (c < 2) {
        return -1;
      }
      ans += (c + 2) / 3;
    }
    return ans;
  }
};
func minOperations(nums []int) (ans int) {
  count := map[int]int{}
  for _, num := range nums {
    count[num]++
  }
  for _, c := range count {
    if c < 2 {
      return -1
    }
    ans += (c + 2) / 3
  }
  return
}
function minOperations(nums: number[]): number {
  const count: Map<number, number> = new Map();
  for (const num of nums) {
    count.set(num, (count.get(num) ?? 0) + 1);
  }
  let ans = 0;
  for (const [_, c] of count) {
    if (c < 2) {
      return -1;
    }
    ans += ((c + 2) / 3) | 0;
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文