-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperUglyNumber.java
63 lines (48 loc) · 1.8 KB
/
SuperUglyNumber.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: Zawad Zamil
* Date: 11/3/24
* Time: 12:53 PM
* Email: zawad@zaagsys.com
*/
public class SuperUglyNumber {
public int nthSuperUglyNumber(int n, int[] primes) {
long[] dp = new long[n];
if (n <= 1)
return 1;
dp[0] = 1;
Map<Integer, Integer> map = new HashMap<>();
for (int prime : primes) {
map.put(prime, 0);
}
// System.out.println(map);
for (int i = 1; i < n; i++) {
Map<Integer, Long> list = new HashMap<>();
map.forEach((prime, index) -> list.put(prime, (long) (dp[index] * prime)));
// Convert the HashMap to a list of Map.Entry objects
List<Map.Entry<Integer, Long>> entryList = new ArrayList<>(list.entrySet());
// Sort the list by values in ascending order
entryList.sort(Map.Entry.comparingByValue());
// Create a new LinkedHashMap to store the sorted entries
LinkedHashMap<Integer, Long> sortedMap = new LinkedHashMap<>();
for (Map.Entry<Integer, Long> entry : entryList) {
sortedMap.put(entry.getKey(), entry.getValue());
}
Integer firstPrime = sortedMap.keySet().stream().findFirst().get();
dp[i] = list.get(firstPrime);
int j = i;
map.forEach((key, value) -> {
if (dp[j] % key == 0) {
map.replace(key, value + 1);
}
});
}
// System.out.println(Arrays.toString(dp));
return (int) dp[n - 1];
}
public static void main(String[] args) {
SuperUglyNumber sup = new SuperUglyNumber();
System.out.println(sup.nthSuperUglyNumber(5911, new int[]{2, 3, 5, 7}));
}
}