-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
33 lines (33 loc) · 790 Bytes
/
Solution.cs
File metadata and controls
33 lines (33 loc) · 790 Bytes
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
public class Solution
{
public string GetPermutation(int n, int k)
{
string ans = "";
int[] fact = new int[n + 1];
bool[] seen = new bool[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++)
{
fact[i] = fact[i - 1] * i;
}
for (int i = 0; i < n; i++)
{
int count = fact[n - i - 1];
for (int d = 1; d <= n; d++)
{
if (seen[d]) continue;
if (k > count)
{
k -= count;
}
else
{
ans += (char)('0' + d);
seen[d] = true;
break;
}
}
}
return ans;
}
}