hello-algo/en/codes/c/chapter_backtracking/permutations_ii.c

82 lines
2.4 KiB
C

/**
* File: permutations_ii.c
* Created Time: 2023-10-17
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.h"
// Assume a maximum of 1000 permutations, maximum element size 1000
#define MAX_SIZE 1000
/* Backtracking algorithm: Permutation II */
void backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {
// When the state length equals the number of elements, record the solution
if (stateSize == choicesSize) {
res[*resSize] = (int *)malloc(choicesSize * sizeof(int));
for (int i = 0; i < choicesSize; i++) {
res[*resSize][i] = state[i];
}
(*resSize)++;
return;
}
// Traverse all choices
bool duplicated[MAX_SIZE] = {false};
for (int i = 0; i < choicesSize; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
if (!selected[i] && !duplicated[choice]) {
// Attempt: make a choice, update the state
duplicated[choice] = true; // Record selected element values
selected[i] = true;
state[stateSize] = choice;
// Proceed to the next round of selection
backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);
// Retract: undo the choice, restore to the previous state
selected[i] = false;
}
}
}
/* Permutation II */
int **permutationsII(int *nums, int numsSize, int *returnSize) {
int *state = (int *)malloc(numsSize * sizeof(int));
bool *selected = (bool *)malloc(numsSize * sizeof(bool));
for (int i = 0; i < numsSize; i++) {
selected[i] = false;
}
int **res = (int **)malloc(MAX_SIZE * sizeof(int *));
*returnSize = 0;
backtrack(state, 0, nums, numsSize, selected, res, returnSize);
free(state);
free(selected);
return res;
}
/* Driver Code */
int main() {
int nums[] = {1, 1, 2};
int numsSize = sizeof(nums) / sizeof(nums[0]);
int returnSize;
int **res = permutationsII(nums, numsSize, &returnSize);
printf("Input array nums =");
printArray(nums, numsSize);
printf("\nAll permutations res = \n");
for (int i = 0; i < returnSize; i++) {
printArray(res[i], numsSize);
}
// Free memory
for (int i = 0; i < returnSize; i++) {
free(res[i]);
}
free(res);
return 0;
}