leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

min_ops_uni_val_grid.c (1827B)


// 2033. Minimum Operations to Make a Uni-Value Grid
#include "leetcode.h"

/*
 * you are given a 2d integer 'grid' of size 'm x n' and an integer 'x'. in one
 * operation, you can add 'x' to or subtract 'x' from any element in the 'grid'.
 * a uni-value grid is a grid where all the elements of it are equal. return the
 * minimum number of operations to make the grid uni-valued. if it is not
 * possible, return -1.
 */

int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int minOperations(int **grid, int gridSize, int *gridColSize, int x) {
  int n = gridSize * (*gridColSize), idx = 0, mod = grid[0][0] % x;
  int *vals = (int *)malloc(n * sizeof(int));
  for (int i = 0; i < gridSize; ++i)
    for (int j = 0; j < *gridColSize; ++j) {
      vals[idx] = grid[i][j];
      if (vals[idx++] % x != mod) {
        free(vals);
        return -1;
      }
    }
  qsort(vals, n, sizeof(int), cmp);
  int med = vals[n / 2], ans = 0;
  for (int i = 0; i < n; i++)
    ans += abs(med - vals[i]) / x;
  free(vals);
  return ans;
}

int main() {
  int g1i[2][2] = {{2, 4}, {6, 8}};
  int g2i[2][2] = {{1, 5}, {2, 3}};
  int g3i[2][2] = {{1, 2}, {3, 4}};
  struct two_d_arr *g1 =
      two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i);
  struct two_d_arr *g2 =
      two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i);
  struct two_d_arr *g3 =
      two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i);
  int r1 = minOperations(g1->arr, g1->row_size, g1->col_size, 2);
  int r2 = minOperations(g2->arr, g2->row_size, g2->col_size, 1);
  int r3 = minOperations(g3->arr, g3->row_size, g3->col_size, 2);
  printf("%d\n", r1);
  assert(r1 == 4);
  printf("%d\n", r2);
  assert(r2 == 5);
  printf("%d\n", r3);
  assert(r3 == -1);
  two_d_arr_free(g1);
  two_d_arr_free(g2);
  two_d_arr_free(g3);
}