diff --git a/leetcode/src/78.c b/leetcode/src/78.c new file mode 100644 index 00000000..2ab7d9a5 --- /dev/null +++ b/leetcode/src/78.c @@ -0,0 +1,45 @@ +/** + * 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(). + */ +int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) +{ + int numSubsets = (int)pow(2, numsSize); + + // Allocate memory for the result array + int** result = (int**)malloc(numSubsets * sizeof(int*)); + *returnColumnSizes = (int*)malloc(numSubsets * sizeof(int)); + + // Initialize the sizes of individual subsets + for (int i = 0; i < numSubsets; i++) + { + int subsetSize = 0; + for (int j = 0; j < numsSize; j++) + { + if ((i >> j) & 1) + { + subsetSize++; + } + } + (*returnColumnSizes)[i] = subsetSize; + } + + // Generate all subsets + for (int i = 0; i < numSubsets; i++) + { + result[i] = (int*)malloc((*returnColumnSizes)[i] * sizeof(int)); + int index = 0; + for (int j = 0; j < numsSize; j++) + { + if ((i >> j) & 1) + { + result[i][index++] = nums[j]; + } + } + } + + *returnSize = numSubsets; + return result; +} diff --git a/leetcode/src/90.c b/leetcode/src/90.c new file mode 100644 index 00000000..4670939e --- /dev/null +++ b/leetcode/src/90.c @@ -0,0 +1,61 @@ +/** + * 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(). + */ +int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); } + +// Recursive function to generate subsets with duplicates +void generateSubsetsWithDup(int *nums, int numsSize, int startIndex, + int *currentSubset, int currentSize, int ***result, + int **columnSizes, int *returnSize) +{ + // Add the current subset to the result + (*result) = (int **)realloc((*result), (*returnSize + 1) * sizeof(int *)); + (*columnSizes) = + (int *)realloc((*columnSizes), (*returnSize + 1) * sizeof(int)); + + (*result)[*returnSize] = (int *)malloc(currentSize * sizeof(int)); + memcpy((*result)[*returnSize], currentSubset, currentSize * sizeof(int)); + + (*columnSizes)[*returnSize] = currentSize; + + (*returnSize)++; + + // Generate subsets by including the current element + for (int i = startIndex; i < numsSize; i++) + { + // Skip duplicates + if (i > startIndex && nums[i] == nums[i - 1]) + { + continue; + } + + // Add the current element to the current subset + currentSubset[currentSize] = nums[i]; + generateSubsetsWithDup(nums, numsSize, i + 1, currentSubset, + currentSize + 1, result, columnSizes, + returnSize); + } +} + +int **subsetsWithDup(int *nums, int numsSize, int *returnSize, + int **returnColumnSizes) +{ + // Sort the input array to handle duplicates + qsort(nums, numsSize, sizeof(int), compare); + + int **result = NULL; + int *columnSizes = NULL; + int *currentSubset = (int *)malloc(numsSize * sizeof(int)); + *returnSize = 0; + + generateSubsetsWithDup(nums, numsSize, 0, currentSubset, 0, &result, + &columnSizes, returnSize); + + free(currentSubset); + + *returnColumnSizes = columnSizes; + return result; +}