forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionA.java
More file actions
46 lines (40 loc) · 922 Bytes
/
QuestionA.java
File metadata and controls
46 lines (40 loc) · 922 Bytes
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
44
45
46
package Question7_7;
import java.util.Queue;
import java.util.LinkedList;
public class QuestionA {
public static int removeMin(Queue<Integer> q) {
int min = q.peek();
for (Integer v : q) {
if (min > v) {
min = v;
}
}
while (q.contains(min)) {
q.remove(min);
}
return min;
}
public static void addProducts(Queue<Integer> q, int v) {
q.add(v * 3);
q.add(v * 5);
q.add(v * 7);
}
public static int getKthMagicNumber(int k) {
if (k < 0) {
return 0;
}
int val = 1;
Queue<Integer> q = new LinkedList<Integer>();
addProducts(q, 1);
for (int i = 0; i < k; i++) { // Start at 1 since we've already done one iteration
val = removeMin(q);
addProducts(q, val);
}
return val;
}
public static void main(String[] args) {
for (int i = 0; i < 14; i++) {
System.out.println(i + " : " + getKthMagicNumber(i));
}
}
}