aboutsummaryrefslogtreecommitdiff
path: root/min_num_ops_arr_empty.c
blob: 1b207cb4c8e319d9d4bde6a9e8287d3f0897239e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 2870. Minimum Number of Operations to Make Array Empty
#include "leetcode.h"

/*
 * 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.
 */

int minOperations(int *nums, int nums_size) {
  int ans = 0, ctr[1000001] = {0};
  for (int i = 0; i < nums_size; i++)
    ctr[nums[i]]++;
  for (int i = 0; i < 1000001; i++) {
    if (ctr[i] == 1)
      return -1;
    if (ctr[i]) {
      if (ctr[i] % 3)
        ans += ctr[i] / 3 + 1;
      else
        ans += ctr[i] / 3;
    }
  }
  return ans;
}

int main() {
  int n1[] = {2, 3, 3, 2, 2, 4, 2, 3, 4}, n2[] = {2, 1, 2, 2, 3, 3};
  printf("%d\n", minOperations(n1, ARRAY_SIZE(n1))); // expect:  4
  printf("%d\n", minOperations(n2, ARRAY_SIZE(n2))); // expect: -1
}