Skip to content

Commit 975e8ac

Browse files
committed
Synced from java to cpp
1 parent 1431403 commit 975e8ac

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

‎cpp/SUMMARY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
* [Happy Number](number-theory/happy-number.md)
223223
* [Ugly Number](number-theory/ugly-number.md)
224224
* [Ugly Number II](number-theory/ugly-number-ii.md)
225+
* [Super Ugly Number](number-theory/super-ugly-number.md)
225226
* [Fraction to Recurring Decimal](number-theory/fraction-to-recurring-decimal.md)
226227
* [Factorial Trailing Zeroes](number-theory/factorial-trailing-zeroes.md)
227228
* [Nim Game](number-theory/nim-game.md)

‎cpp/code/super-ugly-number.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Super Ugly Number
2+
// Time complexity: O(n), Space complexity: O(n)
3+
public class Solution {
4+
public int nthSuperUglyNumber(int n, int[] primes) {
5+
final int[] nums = new int[n];
6+
nums[0] = 1; // 1 is the first ugly number
7+
final Queue<Node> q = new PriorityQueue<>();
8+
for (int i = 0; i < primes.length; ++i) {
9+
q.add(new Node(0, primes[i], primes[i]));
10+
}
11+
12+
for (int i = 1; i < n; ++i) {
13+
// get the min element and add to nums
14+
Node node = q.peek();
15+
nums[i] = node.val;
16+
17+
// update top elements
18+
do {
19+
node = q.poll();
20+
node.val = nums[++node.index] * node.prime;
21+
q.add(node); // push it back
22+
// prevent duplicate
23+
} while (!q.isEmpty() && q.peek().val == nums[i]);
24+
}
25+
return nums[n - 1];
26+
}
27+
static class Node implements Comparable<Node> {
28+
private int index;
29+
private int val;
30+
private int prime;
31+
32+
public Node(int index, int val, int prime) {
33+
this.index = index;
34+
this.val = val;
35+
this.prime = prime;
36+
}
37+
public int compareTo(Node other) {
38+
return this.val - other.val;
39+
}
40+
}
41+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## Super Ugly Number
2+
3+
4+
### 描述
5+
6+
Write a function to find the `n`-th super ugly number.
7+
8+
Super ugly numbers are positive numbers whose all prime factors are in the given prime list `primes` of size `k`. For example, `[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]` is the sequence of the first 12 super ugly numbers given `primes = [2, 7, 13, 19]` of size 4.
9+
10+
**Note**:
11+
12+
1. 1 is a super ugly number for any given `primes`.
13+
2. The given numbers in `primes` are in ascending order.
14+
3. 0 < `k` ≤ 100, 0 < `n` ≤ 1000000, 0 < `primes[i]` < 1000.
15+
16+
17+
### 分析
18+
19+
这题是 [Ugly Number II](ugly-number-ii.md) 的扩展。在"Ugly Number II"中,`primes=[2,3,5]`,这题中`primes`可以自由变化。
20+
21+
所以这题可以用"Ugly Number II"的思路解决。每次要从多个列表中选择最小的元素,我们可以维护一个大小为`primes`长度的小根堆。
22+
23+
24+
### 代码
25+
26+
{% if book.java %}
27+
```java
28+
// Super Ugly Number
29+
// Time complexity: O(n), Space complexity: O(n)
30+
public class Solution {
31+
public int nthSuperUglyNumber(int n, int[] primes) {
32+
final int[] nums = new int[n];
33+
nums[0] = 1; // 1 is the first ugly number
34+
final Queue<Node> q = new PriorityQueue<>();
35+
for (int i = 0; i < primes.length; ++i) {
36+
q.add(new Node(0, primes[i], primes[i]));
37+
}
38+
39+
for (int i = 1; i < n; ++i) {
40+
// get the min element and add to nums
41+
Node node = q.peek();
42+
nums[i] = node.val;
43+
44+
// update top elements
45+
do {
46+
node = q.poll();
47+
node.val = nums[++node.index] * node.prime;
48+
q.add(node); // push it back
49+
// prevent duplicate
50+
} while (!q.isEmpty() && q.peek().val == nums[i]);
51+
}
52+
return nums[n - 1];
53+
}
54+
static class Node implements Comparable<Node> {
55+
private int index;
56+
private int val;
57+
private int prime;
58+
59+
public Node(int index, int val, int prime) {
60+
this.index = index;
61+
this.val = val;
62+
this.prime = prime;
63+
}
64+
public int compareTo(Node other) {
65+
return this.val - other.val;
66+
}
67+
}
68+
}
69+
```
70+
{% endif %}
71+
72+
73+
### 相关题目
74+
75+
* [Ugly Number II](ugly-number-ii.md)

‎cpp/number-theory/ugly-number-ii.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ Note that 1 is typically treated as an ugly number.
3838
### 相关题目
3939

4040
* [Ugly Number](ugly-number.md)
41+
* [Super Ugly Number](super-ugly-number.md)

0 commit comments

Comments
 (0)