-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.java
More file actions
43 lines (35 loc) · 1.26 KB
/
Permutation.java
File metadata and controls
43 lines (35 loc) · 1.26 KB
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
34
35
36
37
38
39
40
41
42
43
/* *****************************************************************************
* Name: Jaya Mukherjee
* Coursera User ID: 123456
* Last modified: June 24, 2021
**************************************************************************** */
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Iterator;
public class Permutation {
public static void main(String[] args) {
if (args[0] != null) {
int num = Integer.parseInt(args[0]);
RandomizedQueue<String> r = new RandomizedQueue<String>();
int index = 0;
while (!StdIn.isEmpty() && num > 0) {
String input = StdIn.readString();
index++;
if ((index > num) && StdRandom.bernoulli((double) num / index)) {
r.dequeue();
}
else if (index > num) {
continue;
}
r.enqueue(input);
}
int i = 0;
Iterator<String> iter = r.iterator();
while (iter.hasNext() && (i < num)) {
StdOut.println(iter.next() + " ");
i++;
}
}
}
}