mirror of
https://gitlab.com/mfocko/CodeWars.git
synced 2024-11-22 16:43:46 +01:00
27 lines
588 B
Java
27 lines
588 B
Java
|
import java.util.PriorityQueue;
|
||
|
|
||
|
public class Hamming {
|
||
|
private static void update(PriorityQueue<Long> q, long n) {
|
||
|
q.add(2*n);
|
||
|
q.add(3*n);
|
||
|
q.add(5*n);
|
||
|
}
|
||
|
|
||
|
public static long hamming(int n) {
|
||
|
PriorityQueue<Long> cache = new PriorityQueue<>();
|
||
|
update(cache, 1);
|
||
|
|
||
|
long smallest = 1;
|
||
|
for (int i = 1; i < n; i++) {
|
||
|
smallest = cache.poll();
|
||
|
while (cache.peek() == smallest) {
|
||
|
cache.poll();
|
||
|
}
|
||
|
|
||
|
update(cache, smallest);
|
||
|
}
|
||
|
|
||
|
return smallest;
|
||
|
}
|
||
|
}
|