aboutsummaryrefslogtreecommitdiff
path: root/min_num_ops_ball_box.c
blob: a6cba59a35f62efc683853b523d11a2c7f84cb9f (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
// 1769. Minimum Number of Operations to Move All Balls to Each Box
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int *minOperations(char *boxes, int *returnSize) {
  int len = strlen(boxes), steps;
  int *ans = malloc(len * sizeof(int));
  for (int i = 0; i < len; i++) {
    steps = 0;
    for (int j = 0; j < len; j++)
      if (boxes[j] == '1')
        steps += abs(i - j);
    ans[i] = steps;
  }
  *returnSize = len;
  return ans;
}

int main() {
  char boxes1[] = {"110"}, boxes2[] = {"001011"};
  int rs1[] = {3}, rs2[] = {6};
  printf("%c\n", *minOperations(boxes1, rs1)); // expect: 1,1,3
  printf("%c\n", *minOperations(boxes2, rs2)); // expect: 11,8,5,4,3,4
}