-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBadNumbers.java
More file actions
38 lines (35 loc) · 1.08 KB
/
BadNumbers.java
File metadata and controls
38 lines (35 loc) · 1.08 KB
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
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.Arrays;
public class BadNumbers {
public List<Integer> generate(int n) {
if (n <= 0)
return new LinkedList<>();
Queue<Integer> cache = new PriorityQueue<>(Arrays.asList(1, 2, 3, 5));
List<Integer> nums = new LinkedList<>();
int multiplier = 2;
int count = 0;
while (count <= n) {
int num2 = 2 * multiplier;
int num3 = 3 * (multiplier + 1);
int num5 = 5 * (multiplier + 2);
cache.add(num2);
if (num3 % 2 != 0)
cache.add(num3);
if (num5 % 2 != 0 && num5 % 3 != 0)
cache.add(num5);
nums.add(cache.remove());
multiplier++;
count++;
}
return nums;
}
public static void main(String[] args) {
int n = 100;
BadNumbers bn = new BadNumbers();
List<Integer> nums = bn.generate(n);
System.out.println("nums: " + nums);
}
}