Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d30caf5

Browse files
kth permutation seq
1 parent 234bc96 commit d30caf5

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class kthPermutationSequenceOptimalSolution {
5+
static BufferedReader br;
6+
static StringTokenizer st;
7+
8+
static int nextInt() throws IOException {
9+
return Integer.parseInt(next());
10+
}
11+
12+
static String next() throws IOException {
13+
while (st == null || !st.hasMoreTokens()) {
14+
st = new StringTokenizer(br.readLine());
15+
}
16+
return st.nextToken();
17+
}
18+
19+
static long nextLong() throws IOException {
20+
return Long.parseLong(next());
21+
}
22+
23+
public static void main(String args[]) throws IOException {
24+
br = new BufferedReader(new InputStreamReader(System.in));
25+
int t = nextInt();
26+
while (t-- > 0) {
27+
input();
28+
}
29+
br.close();
30+
}
31+
32+
public static void input() throws IOException {
33+
int n = nextInt();
34+
int k = nextInt();
35+
System.out.println(getPermutation(n, k));
36+
}
37+
38+
static String getPermutation(int n, int k) {
39+
int fact = 1;
40+
List<Integer> numbers = new ArrayList<>();
41+
for (int i = 1; i < n; i++) {
42+
fact = fact * i;
43+
numbers.add(i);
44+
}
45+
numbers.add(n);
46+
String ans = "";
47+
k = k - 1;
48+
while (true) {
49+
ans = ans + "" + numbers.get(k / fact);
50+
numbers.remove(k / fact);
51+
if (numbers.size() == 0) {
52+
break;
53+
}
54+
k = k % fact;
55+
fact = fact / numbers.size();
56+
}
57+
58+
return ans;
59+
}
60+
}

‎DSAPractice/recursion/PermutationSequence2.java‎ renamed to ‎DSAPractice/recursion/kthPermutationSequenceUsingCharArray.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import java.util.*;
33

44
//https://leetcode.com/problems/permutation-sequence/
5-
public class PermutationSequence2 {
5+
public class kthPermutationSequenceUsingCharArray {
66
static BufferedReader br;
77
static StringTokenizer st;
8-
PermutationSequence ps = new PermutationSequence();
8+
kthPermutationSequenceUsingString ps = new kthPermutationSequenceUsingString();
99

1010
static int nextInt() throws IOException {
1111
return Integer.parseInt(next());

‎DSAPractice/recursion/PermutationSequence.java‎ renamed to ‎DSAPractice/recursion/kthPermutationSequenceUsingString.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import java.util.*;
33

44
//https://leetcode.com/problems/permutation-sequence/
5-
public class PermutationSequence {
5+
public class kthPermutationSequenceUsingString {
66
static BufferedReader br;
77
static StringTokenizer st;
8-
PermutationSequence ps = new PermutationSequence();
8+
kthPermutationSequenceUsingString ps = new kthPermutationSequenceUsingString();
99

1010
static int nextInt() throws IOException {
1111
return Integer.parseInt(next());

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /