-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc0077.c
46 lines (32 loc) · 991 Bytes
/
lc0077.c
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
40
41
42
43
44
45
46
// Licensed under the MIT License.
// Combinations
#include <string.h>
#include "../lib/binomial.h"
#include "../lib/combination_iterator.h"
#include "../lib/euler.h"
int** combine(int n, int k, int* returnSize, int** returnColumnSizes)
{
int i = 0;
int nCK = binomial(n, k);
struct CombinationIterator it;
int** results = malloc(nCK * sizeof * results);
euler_assert(results);
int* subset = malloc(k * sizeof * subset);
euler_assert(subset);
for (combination_begin(&it, subset, n, k); !it.end; combination_next(&it))
{
results[i] = malloc(k * sizeof * results[i]);
euler_assert(results[i]);
memcpy(results[i], subset, k * sizeof * subset);
i++;
}
free(subset);
*returnSize = nCK;
*returnColumnSizes = malloc(nCK * sizeof ** returnColumnSizes);
euler_assert(*returnColumnSizes);
for (i = 0; i < nCK; i++)
{
returnColumnSizes[0][i] = k;
}
return results;
}