MCPcopy Create free account
hub / github.com/MolinDeng/Princeton-algs4 / Permutation

Class Permutation

02Lab-Queues/Permutation.java:11–31  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9import edu.princeton.cs.algs4.StdRandom;
10
11public class Permutation {
12 public static void main(String[] args) {
13 int k = Integer.parseInt(args[0]);
14 if (k == 0) return;
15 RandomizedQueue<String> queue = new RandomizedQueue<>();
16 int cnt = 0;
17 while (!StdIn.isEmpty()) {
18 String s = StdIn.readString();
19 cnt++;
20 if (queue.size() == k) {
21 if (StdRandom.bernoulli(1.0 * k / cnt)) {
22 queue.dequeue();
23 queue.enqueue(s);
24 }
25 }
26 else
27 queue.enqueue(s);
28 }
29 while (k-- > 0) StdOut.println(queue.dequeue());
30 }
31}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected