Skip to content

Commit 0bb9cb4

Browse files
committed
feat: add solutions to lc problem: No.0970
No.0970.Powerful Integers
1 parent 01c82a2 commit 0bb9cb4

File tree

8 files changed

+277
-73
lines changed

8 files changed

+277
-73
lines changed

solution/0900-0999/0970.Powerful Integers/README.md

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:哈希表 + 枚举**
52+
53+
根据题目描述,一个强整数可以表示成 $x^i + y^j$,其中 $i \geq 0$, $j \geq 0$。
54+
55+
题目需要我们找出所有不超过 $bound$ 的强整数,我们注意到 $bound$ 的取值范围不超过 $10^6$,而 $2^{20} = 1048576 \gt 10^6$。因此,如果 $x \geq 2$,那么 $i$ 最大不超过 $20$,才有可能使得 $x^i + y^j \leq bound$ 成立。同理,如果 $y \geq 2$,那么 $j$ 最大不超过 $20$。
56+
57+
因此我们可以使用双重循环,枚举所有可能的 $x^i$ 和 $y^j$,分别记为 $a$ 和 $b$,并保证 $a + b \leq bound$,此时 $a + b$ 即为一个强整数。我们使用哈希表存储所有满足条件的强整数,最后将哈希表中的所有元素转换成答案列表返回即可。
58+
59+
> 注意,如果 $x=1$ 或者 $y=1$,那么 $a$ 或者 $b$ 的值恒等于 $1$,对应的循环只需要执行一次即可退出。
60+
61+
时间复杂度 $O(\log^2 bound)$,空间复杂度 $O(\log^2 bound)$。
62+
5163
<!-- tabs:start -->
5264

5365
### **Python3**
@@ -57,20 +69,19 @@
5769
```python
5870
class Solution:
5971
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
60-
s = set()
61-
i = 1
62-
while i < bound:
63-
j = 1
64-
while j < bound:
65-
if i + j <= bound:
66-
s.add(i + j)
72+
ans = set()
73+
a = 1
74+
while a <= bound:
75+
b = 1
76+
while a + b <= bound:
77+
ans.add(a + b)
78+
b *= y
6779
if y == 1:
6880
break
69-
j *= y
7081
if x == 1:
7182
break
72-
i *= x
73-
return list(s)
83+
a *= x
84+
return list(ans)
7485
```
7586

7687
### **Java**
@@ -80,12 +91,33 @@ class Solution:
8091
```java
8192
class Solution {
8293
public List<Integer> powerfulIntegers(int x, int y, int bound) {
83-
Set<Integer> s = new HashSet<>();
84-
for (int i = 1; i < bound; i *= x) {
85-
for (int j = 1; j < bound; j *= y) {
86-
if (i + j <= bound) {
87-
s.add(i + j);
94+
Set<Integer> ans = new HashSet<>();
95+
for (int a = 1; a <= bound; a *= x) {
96+
for (int b = 1; a + b <= bound; b *= y) {
97+
ans.add(a + b);
98+
if (y == 1) {
99+
break;
88100
}
101+
}
102+
if (x == 1) {
103+
break;
104+
}
105+
}
106+
return new ArrayList<>(ans);
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
vector<int> powerfulIntegers(int x, int y, int bound) {
117+
unordered_set<int> ans;
118+
for (int a = 1; a <= bound; a *= x) {
119+
for (int b = 1; a + b <= bound; b *= y) {
120+
ans.insert(a + b);
89121
if (y == 1) {
90122
break;
91123
}
@@ -94,8 +126,51 @@ class Solution {
94126
break;
95127
}
96128
}
97-
return new ArrayList<>(s);
129+
return vector<int>(ans.begin(), ans.end());
130+
}
131+
};
132+
```
133+
134+
### **Go**
135+
136+
```go
137+
func powerfulIntegers(x int, y int, bound int) (ans []int) {
138+
s := map[int]struct{}{}
139+
for a := 1; a <= bound; a *= x {
140+
for b := 1; a+b <= bound; b *= y {
141+
s[a+b] = struct{}{}
142+
if y == 1 {
143+
break
144+
}
145+
}
146+
if x == 1 {
147+
break
148+
}
149+
}
150+
for x := range s {
151+
ans = append(ans, x)
152+
}
153+
return ans
154+
}
155+
```
156+
157+
### **TypeScript**
158+
159+
```ts
160+
function powerfulIntegers(x: number, y: number, bound: number): number[] {
161+
const ans = new Set<number>();
162+
for (let a = 1; a <= bound; a *= x) {
163+
for (let b = 1; a + b <= bound; b *= y) {
164+
ans.add(a + b);
165+
if (y === 1) {
166+
break;
167+
}
168+
}
169+
if (x === 1) {
170+
break;
171+
}
98172
}
173+
return Array.from(ans);
99174
}
100175
```
101176

@@ -109,17 +184,19 @@ class Solution {
109184
* @return {number[]}
110185
*/
111186
var powerfulIntegers = function (x, y, bound) {
112-
let res = new Set();
113-
for (let i = 1; i < bound; i *= x) {
114-
for (let j = 1; j < bound; j *= y) {
115-
if (i + j <= bound) {
116-
res.add(i + j);
187+
const ans = new Set();
188+
for (let a = 1; a <= bound; a *= x) {
189+
for (let b = 1; a + b <= bound; b *= y) {
190+
ans.add(a + b);
191+
if (y === 1) {
192+
break;
117193
}
118-
if (y == 1) break;
119194
}
120-
if (x == 1) break;
195+
if (x === 1) {
196+
break;
197+
}
121198
}
122-
return [...res];
199+
return [...ans];
123200
};
124201
```
125202

solution/0900-0999/0970.Powerful Integers/README_EN.md

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,72 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Hash Table + Enumeration**
47+
48+
According to the description of the problem, a powerful integer can be represented as $x^i + y^j$, where $i \geq 0$, $j \geq 0$.
49+
50+
The problem requires us to find all powerful integers that do not exceed $bound$. We notice that the value range of $bound$ does not exceed $10^6$, and $2^{20} = 1048576 \gt 10^6$. Therefore, if $x \geq 2$, then $i$ is at most $20$ to make $x^i + y^j \leq bound$ hold. Similarly, if $y \geq 2$, then $j$ is at most $20$.
51+
52+
Therefore, we can use double loop to enumerate all possible $x^i$ and $y^j$, denoted as $a$ and $b$ respectively, and ensure that $a + b \leq bound$, then $a + b$ is a powerful integer. We use a hash table to store all powerful integers that meet the conditions, and finally convert all elements in the hash table into the answer list and return it.
53+
54+
> Note that if $x=1$ or $y=1$, then the value of $a$ or $b$ is always equal to $1$, and the corresponding loop only needs to be executed once to exit.
55+
56+
The time complexity is $O(\log^2 bound)$, and the space complexity is $O(\log^2 bound)$.
57+
4658
<!-- tabs:start -->
4759

4860
### **Python3**
4961

5062
```python
5163
class Solution:
5264
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
53-
s = set()
54-
i = 1
55-
while i < bound:
56-
j = 1
57-
while j < bound:
58-
if i + j <= bound:
59-
s.add(i + j)
65+
ans = set()
66+
a = 1
67+
while a <= bound:
68+
b = 1
69+
while a + b <= bound:
70+
ans.add(a + b)
71+
b *= y
6072
if y == 1:
6173
break
62-
j *= y
6374
if x == 1:
6475
break
65-
i *= x
66-
return list(s)
76+
a *= x
77+
return list(ans)
6778
```
6879

6980
### **Java**
7081

7182
```java
7283
class Solution {
7384
public List<Integer> powerfulIntegers(int x, int y, int bound) {
74-
Set<Integer> s = new HashSet<>();
75-
for (int i = 1; i < bound; i *= x) {
76-
for (int j = 1; j < bound; j *= y) {
77-
if (i + j <= bound) {
78-
s.add(i + j);
85+
Set<Integer> ans = new HashSet<>();
86+
for (int a = 1; a <= bound; a *= x) {
87+
for (int b = 1; a + b <= bound; b *= y) {
88+
ans.add(a + b);
89+
if (y == 1) {
90+
break;
7991
}
92+
}
93+
if (x == 1) {
94+
break;
95+
}
96+
}
97+
return new ArrayList<>(ans);
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
vector<int> powerfulIntegers(int x, int y, int bound) {
108+
unordered_set<int> ans;
109+
for (int a = 1; a <= bound; a *= x) {
110+
for (int b = 1; a + b <= bound; b *= y) {
111+
ans.insert(a + b);
80112
if (y == 1) {
81113
break;
82114
}
@@ -85,8 +117,51 @@ class Solution {
85117
break;
86118
}
87119
}
88-
return new ArrayList<>(s);
120+
return vector<int>(ans.begin(), ans.end());
121+
}
122+
};
123+
```
124+
125+
### **Go**
126+
127+
```go
128+
func powerfulIntegers(x int, y int, bound int) (ans []int) {
129+
s := map[int]struct{}{}
130+
for a := 1; a <= bound; a *= x {
131+
for b := 1; a+b <= bound; b *= y {
132+
s[a+b] = struct{}{}
133+
if y == 1 {
134+
break
135+
}
136+
}
137+
if x == 1 {
138+
break
139+
}
140+
}
141+
for x := range s {
142+
ans = append(ans, x)
143+
}
144+
return ans
145+
}
146+
```
147+
148+
### **TypeScript**
149+
150+
```ts
151+
function powerfulIntegers(x: number, y: number, bound: number): number[] {
152+
const ans = new Set<number>();
153+
for (let a = 1; a <= bound; a *= x) {
154+
for (let b = 1; a + b <= bound; b *= y) {
155+
ans.add(a + b);
156+
if (y === 1) {
157+
break;
158+
}
159+
}
160+
if (x === 1) {
161+
break;
162+
}
89163
}
164+
return Array.from(ans);
90165
}
91166
```
92167

@@ -100,17 +175,19 @@ class Solution {
100175
* @return {number[]}
101176
*/
102177
var powerfulIntegers = function (x, y, bound) {
103-
let res = new Set();
104-
for (let i = 1; i < bound; i *= x) {
105-
for (let j = 1; j < bound; j *= y) {
106-
if (i + j <= bound) {
107-
res.add(i + j);
178+
const ans = new Set();
179+
for (let a = 1; a <= bound; a *= x) {
180+
for (let b = 1; a + b <= bound; b *= y) {
181+
ans.add(a + b);
182+
if (y === 1) {
183+
break;
108184
}
109-
if (y == 1) break;
110185
}
111-
if (x == 1) break;
186+
if (x === 1) {
187+
break;
188+
}
112189
}
113-
return [...res];
190+
return [...ans];
114191
};
115192
```
116193

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> powerfulIntegers(int x, int y, int bound) {
4+
unordered_set<int> ans;
5+
for (int a = 1; a <= bound; a *= x) {
6+
for (int b = 1; a + b <= bound; b *= y) {
7+
ans.insert(a + b);
8+
if (y == 1) {
9+
break;
10+
}
11+
}
12+
if (x == 1) {
13+
break;
14+
}
15+
}
16+
return vector<int>(ans.begin(), ans.end());
17+
}
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func powerfulIntegers(x int, y int, bound int) (ans []int) {
2+
s := map[int]struct{}{}
3+
for a := 1; a <= bound; a *= x {
4+
for b := 1; a+b <= bound; b *= y {
5+
s[a+b] = struct{}{}
6+
if y == 1 {
7+
break
8+
}
9+
}
10+
if x == 1 {
11+
break
12+
}
13+
}
14+
for x := range s {
15+
ans = append(ans, x)
16+
}
17+
return ans
18+
}

0 commit comments

Comments
 (0)