Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit af34ba2

Browse files
authored
feat: add solutions to lc problem: No.0313 (#857)
No.0313.Super Ugly Number
1 parent 421b630 commit af34ba2

File tree

4 files changed

+152
-9
lines changed

4 files changed

+152
-9
lines changed

‎solution/0300-0399/0313.Super Ugly Number/README.md‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,71 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:优先队列**
55+
56+
根据题目要求,模拟第 1 〜 n 个丑数,显然任意一个丑数 `ugly[i]` 乘以 `primes` 数组中的质数 `primes[i]` 仍旧是丑数。已知 `ugly[1]=1`,`ugly[2] = min(ugly[1]*primes[i])`,则 `ugly[3]` 应该等于集合 `ugly[2]*primes[i], ugly[1]*primes[i]` 中排除 `ugly[2]` 之后的最小值,依次类推可以得到 `ugly[n]` 的值。
57+
5458
<!-- tabs:start -->
5559

5660
### **Python3**
5761

5862
<!-- 这里可写当前语言的特殊实现逻辑 -->
5963

6064
```python
61-
65+
from queue import PriorityQueue
66+
67+
68+
class Solution:
69+
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
70+
ugly, pq, p = [0]*(n+1), PriorityQueue(), 2
71+
ugly[1] = 1
72+
for prime in primes:
73+
pq.put([prime, prime, 2])
74+
75+
while p <= n:
76+
top = pq.get()
77+
if top[0] != ugly[p-1]:
78+
ugly[p], p = top[0], p+1
79+
top[0], top[2] = ugly[top[2]]*top[1], top[2]+1
80+
pq.put(top)
81+
return ugly[n]
6282
```
6383

64-
### **Java**
84+
### **Go**
6585

6686
<!-- 这里可写当前语言的特殊实现逻辑 -->
6787

68-
```java
69-
88+
```go
89+
type Ugly struct{ value, prime, index int }
90+
type Queue []Ugly
91+
92+
func (u Queue) Len() int { return len(u) }
93+
func (u Queue) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
94+
func (u Queue) Less(i, j int) bool { return u[i].value < u[j].value }
95+
func (u *Queue) Push(v interface{}) { *u = append(*u, v.(Ugly)) }
96+
func (u *Queue) Pop() interface{} {
97+
old, x := *u, (*u)[len(*u)-1]
98+
*u = old[:len(old)-1]
99+
return x
100+
}
101+
102+
func nthSuperUglyNumber(n int, primes []int) int {
103+
ugly, pq, p := make([]int, n+1), &Queue{}, 2
104+
ugly[1] = 1
105+
heap.Init(pq)
106+
for _, v := range primes {
107+
heap.Push(pq, Ugly{value: v, prime: v, index: 2})
108+
}
109+
for p <= n {
110+
top := heap.Pop(pq).(Ugly)
111+
if ugly[p-1] != top.value {
112+
ugly[p], p = top.value, p+1
113+
}
114+
top.value, top.index = ugly[top.index]*top.prime, top.index+1
115+
heap.Push(pq, top)
116+
}
117+
return ugly[n]
118+
}
70119
```
71120

72121
### **...**

‎solution/0300-0399/0313.Super Ugly Number/README_EN.md‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,65 @@
4040

4141
## Solutions
4242

43+
Priority Queue.
44+
4345
<!-- tabs:start -->
4446

4547
### **Python3**
4648

4749
```python
48-
50+
from queue import PriorityQueue
51+
52+
53+
class Solution:
54+
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
55+
ugly, pq, p = [0]*(n+1), PriorityQueue(), 2
56+
ugly[1] = 1
57+
for prime in primes:
58+
pq.put([prime, prime, 2])
59+
60+
while p <= n:
61+
top = pq.get()
62+
if top[0] != ugly[p-1]:
63+
ugly[p], p = top[0], p+1
64+
top[0], top[2] = ugly[top[2]]*top[1], top[2]+1
65+
pq.put(top)
66+
return ugly[n]
4967
```
5068

51-
### **Java**
52-
53-
```java
54-
69+
### **Go**
70+
71+
```go
72+
type Ugly struct{ value, prime, index int }
73+
type Queue []Ugly
74+
75+
func (u Queue) Len() int { return len(u) }
76+
func (u Queue) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
77+
func (u Queue) Less(i, j int) bool { return u[i].value < u[j].value }
78+
func (u *Queue) Push(v interface{}) { *u = append(*u, v.(Ugly)) }
79+
func (u *Queue) Pop() interface{} {
80+
old, x := *u, (*u)[len(*u)-1]
81+
*u = old[:len(old)-1]
82+
return x
83+
}
84+
85+
func nthSuperUglyNumber(n int, primes []int) int {
86+
ugly, pq, p := make([]int, n+1), &Queue{}, 2
87+
ugly[1] = 1
88+
heap.Init(pq)
89+
for _, v := range primes {
90+
heap.Push(pq, Ugly{value: v, prime: v, index: 2})
91+
}
92+
for p <= n {
93+
top := heap.Pop(pq).(Ugly)
94+
if ugly[p-1] != top.value {
95+
ugly[p], p = top.value, p+1
96+
}
97+
top.value, top.index = ugly[top.index]*top.prime, top.index+1
98+
heap.Push(pq, top)
99+
}
100+
return ugly[n]
101+
}
55102
```
56103

57104
### **...**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
type Ugly struct{ value, prime, index int }
2+
type Queue []Ugly
3+
4+
func (u Queue) Len() int { return len(u) }
5+
func (u Queue) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
6+
func (u Queue) Less(i, j int) bool { return u[i].value < u[j].value }
7+
func (u *Queue) Push(v interface{}) { *u = append(*u, v.(Ugly)) }
8+
func (u *Queue) Pop() interface{} {
9+
old, x := *u, (*u)[len(*u)-1]
10+
*u = old[:len(old)-1]
11+
return x
12+
}
13+
14+
func nthSuperUglyNumber(n int, primes []int) int {
15+
ugly, pq, p := make([]int, n+1), &Queue{}, 2
16+
ugly[1] = 1
17+
heap.Init(pq)
18+
for _, v := range primes {
19+
heap.Push(pq, Ugly{value: v, prime: v, index: 2})
20+
}
21+
for p <= n {
22+
top := heap.Pop(pq).(Ugly)
23+
if ugly[p-1] != top.value {
24+
ugly[p], p = top.value, p+1
25+
}
26+
top.value, top.index = ugly[top.index]*top.prime, top.index+1
27+
heap.Push(pq, top)
28+
}
29+
return ugly[n]
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from queue import PriorityQueue
2+
3+
4+
class Solution:
5+
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
6+
ugly, pq, p = [0]*(n+1), PriorityQueue(), 2
7+
ugly[1] = 1
8+
for prime in primes:
9+
pq.put([prime, prime, 2])
10+
11+
while p <= n:
12+
top = pq.get()
13+
if top[0] != ugly[p-1]:
14+
ugly[p], p = top[0], p+1
15+
top[0], top[2] = ugly[top[2]]*top[1], top[2]+1
16+
pq.put(top)
17+
return ugly[n]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /