| 9 | import edu.princeton.cs.algs4.StdRandom; |
| 10 | |
| 11 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected