| 13 | } |
| 14 | |
| 15 | public static int getKthMagicNumber(int k) { |
| 16 | if (k < 0) { |
| 17 | return 0; |
| 18 | } |
| 19 | int val = 0; |
| 20 | Queue<Integer> queue3 = new LinkedList<Integer>(); |
| 21 | Queue<Integer> queue5 = new LinkedList<Integer>(); |
| 22 | Queue<Integer> queue7 = new LinkedList<Integer>(); |
| 23 | queue3.add(1); |
| 24 | for (int i = 0; i <= k; i++) { // Include 0th iteration through kth iteration |
| 25 | int v3 = queue3.size() > 0 ? queue3.peek() : Integer.MAX_VALUE; |
| 26 | int v5 = queue5.size() > 0 ? queue5.peek() : Integer.MAX_VALUE; |
| 27 | int v7 = queue7.size() > 0 ? queue7.peek() : Integer.MAX_VALUE; |
| 28 | val = Math.min(v3, Math.min(v5, v7)); |
| 29 | if (val == v3) { |
| 30 | queue3.remove(); |
| 31 | queue3.add(3 * val); |
| 32 | queue5.add(5 * val); |
| 33 | } else if (val == v5) { |
| 34 | queue5.remove(); |
| 35 | queue5.add(5 * val); |
| 36 | } else if (val == v7) { |
| 37 | queue7.remove(); |
| 38 | } |
| 39 | queue7.add(7 * val); |
| 40 | } |
| 41 | return val; |
| 42 | } |
| 43 | |
| 44 | public static void main(String[] args) { |
| 45 | for (int i = 0; i < 14; i++) { |