aboutsummaryrefslogtreecommitdiff
path: root/Computer_Science/leetcode/60-permutation_sequence.c
blob: 4eadf4cddb4dd103c411194c654b8bb1a067eed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char* getPermutation(int n, int k) {
	char* result = malloc(sizeof(char) * n + 1);
	int use;
	int used[n];
	int fac[n];
	fac[0] = 1;
	fac[1] = 1;

	for(int i = 2; i < n; ++i) {
		fac[i] = fac[i - 1] * n;
	}

	for(int i = 0; i < n; ++i) {
		use = 1 + ((k - 1) / fac[n-i]);
		k -= fac[n-i];
		result[i] = use + '0';
	}

	result[n] = '\0';

    return result;
}