blob: 72e8648a5c315818ea751911f070ff52bd2c24a7 (
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
|
// 2966. Divide Array Into Arrays With Max Difference
#include "leetcode.h"
/*
* given an integer array 'nums' of size 'n' and a positive integer 'k'. divide
* the array into one or more arrays of size 3 satisfying the following
* conditions. each element of 'nums' should be in exactly one array. the
* different between any two elements in one array is less than or equal to 'k'.
* return a 2d array containing all the arrays if it is impossible to satisfy
* the conditions, return an empty array. and if there are multiple answers,
* return any of them
*/
int cmp(const void *a, const void *b) { return (*(int *)a) - (*(int *)b); }
int **divideArray(int *nums, int numsSize, int k, int *returnSize,
int **returnColumnSizes) {
int num_arr = numsSize / 3;
int **ans = (int **)calloc(num_arr, sizeof(int *));
*returnColumnSizes = (int *)calloc(num_arr, sizeof(int));
*returnSize = 0;
qsort(nums, numsSize, sizeof(int), cmp);
for (int i = 0; i < num_arr; i++) {
ans[i] = (int *)calloc(3, sizeof(int));
(*returnColumnSizes)[i] = 3;
for (int j = 0; j < 3; j++) {
int idx = i * 3 + j;
if (j && nums[idx] - nums[i * 3] > k)
return ans;
ans[i][j] = nums[idx];
}
}
*returnSize = num_arr;
return ans;
}
|