-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.c
More file actions
158 lines (135 loc) · 4.47 KB
/
Copy path3sum.c
File metadata and controls
158 lines (135 loc) · 4.47 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
#define MAX_SOLUTIONS 1000
// Holds the array of solutions
typedef struct {
int** solutions;
int num_solutions;
} Solutions;
// Encapsulates the indices being used as the current triplet
typedef struct {
int inds[3];
} Indices;
int Compare(void* num_1, void* num_2) {
int* num_1_int = (int*)num_1;
int* num_2_int = (int*)num_2;
if(*num_1_int < *num_2_int) {
return -1;
}
else if(*num_1_int > *num_2_int) {
return 1;
}
else {
return 0;
}
}
void AddToSolutions(Solutions* solutions, int* triplet) {
// Sort to get canonical represetation so we can easily
// check if it already exists in the solution set
qsort(triplet, 3, sizeof(int), Compare);
// Check if solution already exists
for(int i = 0; i < solutions->num_solutions; ++i) {
int* current_solution = solutions->solutions[i];
// Already in solution set, no need to add again
if(current_solution[0] == triplet[0] &&
current_solution[1] == triplet[1] &&
current_solution[2] == triplet[2])
{
return;
}
}
// Not in set, so add
solutions->solutions[solutions->num_solutions] = malloc(3 * sizeof(int));
int* solution_to_add = solutions->solutions[solutions->num_solutions];
solution_to_add[0] = triplet[0];
solution_to_add[1] = triplet[1];
solution_to_add[2] = triplet[2];
solutions->num_solutions++;
}
void EnumerateTriplets(Indices indices, int active_index, int max,
int* nums, Solutions* solutions)
{
while(indices.inds[active_index] < max) {
if((0 < active_index) && (1 < indices.inds[active_index] - indices.inds[active_index - 1])) {
EnumerateTriplets(indices, active_index - 1, indices.inds[active_index],
nums, solutions);
}
else {
int triplet[3] = {
nums[indices.inds[0]],
nums[indices.inds[1]],
nums[indices.inds[2]]
};
if((triplet[0] + triplet[1] + triplet[2]) == 0) {
AddToSolutions(solutions, triplet);
}
}
indices.inds[active_index]++;
}
}
/*
Approach 1:
Enumerate ALL triplets recursively
Add valid and unique solutions to solution set
*/
Solutions CheckAllTriplets(int* nums, int numsSize) {
Solutions s = {0};
s.solutions = malloc(MAX_SOLUTIONS * sizeof(int*));
Indices indices = {0, 1, 2};
EnumerateTriplets(indices, 2, numsSize, nums, &s);
return s;
}
/*
Approach 2:
Enumerate all pairs (n^2)
Binary search for number that would make triplet sum equal to 0
*/
Solutions BinarySearchTechnique(int* nums, int numsSize) {
Solutions s = {0};
s.solutions = malloc(MAX_SOLUTIONS * sizeof(int*));
qsort(nums, numsSize, sizeof(int), Compare);
for(int i = 0; i < numsSize; ++i) {
for(int j = i + 1; j < numsSize; ++j) {
int need_to_find = -1 * (nums[i] + nums[j]);
int* num_found = (int*)bsearch(&need_to_find, nums, numsSize, sizeof(int), Compare);
if(num_found) {
int* i_ptr = nums + i;
int* j_ptr = nums + j;
if(num_found != i_ptr &&
num_found != j_ptr)
{
int triplet[3] = {
nums[i],
nums[j],
*num_found
};
AddToSolutions(&s, triplet);
}
}
}
}
return s;
}
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
if(numsSize < 3) {
*returnSize = 0;
return NULL;
}
/*
Both of these approaches are too slow, but they work
BinarySearchTechnique sometimes can be fast enough to pass
*/
//Solutions s = CheckAllTriplets(nums, numsSize);
Solutions s = BinarySearchTechnique(nums, numsSize);
// All column sizes are len 3
int* column_sizes = malloc(s.num_solutions * sizeof(int));
for(int i = 0; i < s.num_solutions; ++i) {
column_sizes[i] = 3;
}
*returnColumnSizes = column_sizes;
*returnSize = s.num_solutions;
return s.solutions;
}