mirror of
https://gitlab.com/mfocko/CodeWars.git
synced 2024-12-26 14:21:31 +01:00
4kyu: add „Hamming Numbers“
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
70af121c76
commit
851b2cc0bb
2 changed files with 27 additions and 0 deletions
26
4kyu/hamming_numbers/solution.java
Normal file
26
4kyu/hamming_numbers/solution.java
Normal file
|
@ -0,0 +1,26 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -56,6 +56,7 @@
|
|||
### Java
|
||||
|
||||
- [Sum of Intervals](https://www.codewars.com/kata/52b7ed099cdc285c300001cd) - [solution](4kyu/sum_of_intervals)
|
||||
- [Hamming Numbers](https://www.codewars.com/kata/526d84b98f428f14a60008da) - [solution](4kyu/hamming_numbers)
|
||||
|
||||
## 5 kyu
|
||||
|
||||
|
|
Loading…
Reference in a new issue