aboutsummaryrefslogtreecommitdiff
path: root/min_balls_in_bag.c
blob: c95c0ef916005ebde0cf7af235f1215f2fddfb61 (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
35
36
37
38
39
// 1760. Minimum Limit of Balls in a Bag
#include <stdbool.h>
#include <stdio.h>

/*
 * given an integer array 'nums' where the i'th bag contains
 * 'nums[i]' balls. also given an integer 'max_operations'.
 * you can perform the following operation at most 'max_operations'
 * times; take any bag of balls and divide it into two new bags
 * with a positive number of balls. the penalty is the maximum
 * number of balls in a bag. you want to minimise your penalty
 * after the operations. return the minimum possible penalty.
 */

bool work(int *nums, int nums_size, int operation, int min) {
  for (int i = 0; i < nums_size && operation >= 0; i++)
    operation -= (nums[i] - 1) / min;
  return operation >= 0;
}

int minimumSize(int *nums, int nums_size, int max_operations) {
  int i = 1, j = 1000000000, k;
  bool can;
  while (i < j) {
    k = (i + j) / 2;
    can = work(nums, nums_size, max_operations, k);
    if (can)
      j = k;
    else
      i = k + 1;
  }
  return j;
}

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