-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutation.java
39 lines (35 loc) · 1.25 KB
/
Permutation.java
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
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
public class Permutation {
public static void main(String[] args) {
int k = Integer.parseInt(args[0]);
/** while (!StdIn.isEmpty()) {
String value = StdIn.readString();
StdRandom.uniform()
} **/
RandomizedQueue<String> queue = new RandomizedQueue<>();
int counter = 0; // count of items read
// edge case
if (k == 0)
while (!StdIn.isEmpty()) {
StdIn.readString(); // just ignore it lol? idk wat else to do
}
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
counter++;
if (queue.size() < k)
queue.enqueue(item);
else {
// randomly decide to replace other item,,, can i do this? idk lol
int randomNumber = StdRandom.uniformInt(counter); // idk wtf im doing
if (randomNumber < k) {
queue.dequeue();
queue.enqueue(item);
}
}
}
for (int i = 0; i < k && !queue.isEmpty(); i++)
StdOut.println(queue.dequeue());
}
}