Skip to content

Commit e65697d

Browse files
committed
feat: add solutions for lc No.3890
1 parent 88bcdb7 commit e65697d

7 files changed

Lines changed: 491 additions & 6 deletions

File tree

solution/3800-3899/3890.Integers With Multiple Sum of Two Cubes/README.md

Lines changed: 167 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,196 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3890.In
7373

7474
<!-- solution:start -->
7575

76-
### 方法一
76+
### 方法一:预处理 + 二分查找
77+
78+
我们注意到,当 $a$ 或 $b$ 大于 $1000$ 时,式子 $a^3 + b^3 > 10^9$。因此,我们只需要枚举 $1 \leq a \leq b \leq 1000$,统计每个整数 $x = a^3 + b^3$ 的出现次数。最后,筛选出出现次数大于 $1$ 的整数,并按升序排序,得到所有的好整数。
79+
80+
我们将所有好整数预处理出来,存储在数组 $\textit{GOOD}$ 中。对于每个查询,我们使用二分查找找到 $\textit{GOOD}$ 中第一个大于 $n$ 的整数的索引 $idx$,返回 $\textit{GOOD}$ 中前 $idx$ 个整数即可。
81+
82+
时间复杂度为 $O(m^2 + k \log k)$,其中 $m = 1000$ 是枚举的范围,而 $k$ 是好整数的数量。空间复杂度为 $O(k)$。
7783

7884
<!-- tabs:start -->
7985

8086
#### Python3
8187

8288
```python
89+
LIMIT = 10**9
90+
91+
cnt = defaultdict(int)
92+
cubes = [i**3 for i in range(1001)]
8393

94+
for a in range(1, 1001):
95+
for b in range(a, 1001):
96+
x = cubes[a] + cubes[b]
97+
if x > LIMIT:
98+
break
99+
cnt[x] += 1
100+
101+
GOOD = sorted(x for x, v in cnt.items() if v > 1)
102+
103+
104+
class Solution:
105+
def findGoodIntegers(self, n: int) -> list[int]:
106+
idx = bisect_right(GOOD, n)
107+
return GOOD[:idx]
84108
```
85109

86110
#### Java
87111

88112
```java
89-
113+
class Solution {
114+
private static final int LIMIT = (int) 1e9;
115+
private static final List<Integer> GOOD = new ArrayList<>();
116+
117+
static {
118+
Map<Integer, Integer> cnt = new HashMap<>();
119+
int[] cubes = new int[1001];
120+
for (int i = 0; i <= 1000; i++) {
121+
cubes[i] = i * i * i;
122+
}
123+
for (int a = 1; a <= 1000; a++) {
124+
for (int b = a; b <= 1000; b++) {
125+
int x = cubes[a] + cubes[b];
126+
if (x > LIMIT) {
127+
break;
128+
}
129+
cnt.merge(x, 1, Integer::sum);
130+
}
131+
}
132+
for (Map.Entry<Integer, Integer> e : cnt.entrySet()) {
133+
if (e.getValue() > 1) {
134+
GOOD.add(e.getKey());
135+
}
136+
}
137+
138+
Collections.sort(GOOD);
139+
}
140+
141+
public List<Integer> findGoodIntegers(int n) {
142+
int idx = Collections.binarySearch(GOOD, n + 1);
143+
if (idx < 0) {
144+
idx = -idx - 1;
145+
}
146+
return GOOD.subList(0, idx);
147+
}
148+
}
90149
```
91150

92151
#### C++
93152

94153
```cpp
95-
154+
vector<int> GOOD;
155+
156+
auto init = [] {
157+
const int LIMIT = 1e9;
158+
159+
unordered_map<int, int> cnt;
160+
vector<int> cubes(1001);
161+
162+
for (int i = 0; i <= 1000; ++i) {
163+
cubes[i] = i * i * i;
164+
}
165+
166+
for (int a = 1; a <= 1000; ++a) {
167+
for (int b = a; b <= 1000; ++b) {
168+
int x = cubes[a] + cubes[b];
169+
if (x > LIMIT) break;
170+
cnt[x]++;
171+
}
172+
}
173+
174+
for (auto& [x, v] : cnt) {
175+
if (v > 1) {
176+
GOOD.push_back(x);
177+
}
178+
}
179+
180+
sort(GOOD.begin(), GOOD.end());
181+
182+
return 0;
183+
}();
184+
185+
class Solution {
186+
public:
187+
vector<int> findGoodIntegers(int n) {
188+
int idx = upper_bound(GOOD.begin(), GOOD.end(), n) - GOOD.begin();
189+
return vector<int>(GOOD.begin(), GOOD.begin() + idx);
190+
}
191+
};
96192
```
97193
98194
#### Go
99195
100196
```go
197+
var GOOD []int
198+
199+
func init() {
200+
const LIMIT = 1e9
201+
202+
cnt := make(map[int]int)
203+
cubes := make([]int, 1001)
204+
205+
for i := 0; i <= 1000; i++ {
206+
cubes[i] = i * i * i
207+
}
208+
209+
for a := 1; a <= 1000; a++ {
210+
for b := a; b <= 1000; b++ {
211+
x := cubes[a] + cubes[b]
212+
if x > LIMIT {
213+
break
214+
}
215+
cnt[x]++
216+
}
217+
}
218+
219+
for x, v := range cnt {
220+
if v > 1 {
221+
GOOD = append(GOOD, x)
222+
}
223+
}
224+
225+
sort.Ints(GOOD)
226+
}
227+
228+
func findGoodIntegers(n int) []int {
229+
idx := sort.Search(len(GOOD), func(i int) bool {
230+
return GOOD[i] > n
231+
})
232+
return GOOD[:idx]
233+
}
234+
```
235+
236+
#### TypeScript
237+
238+
```ts
239+
const LIMIT = 1e9;
240+
241+
const GOOD: number[] = (() => {
242+
const cnt = new Map<number, number>();
243+
const cubes: number[] = Array.from({ length: 1001 }, (_, i) => i * i * i);
244+
245+
for (let a = 1; a <= 1000; a++) {
246+
for (let b = a; b <= 1000; b++) {
247+
const x = cubes[a] + cubes[b];
248+
if (x > LIMIT) break;
249+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
250+
}
251+
}
252+
253+
const res: number[] = [];
254+
for (const [x, v] of cnt.entries()) {
255+
if (v > 1) res.push(x);
256+
}
257+
258+
res.sort((a, b) => a - b);
259+
return res;
260+
})();
101261

262+
function findGoodIntegers(n: number): number[] {
263+
const idx = _.sortedLastIndex(GOOD, n);
264+
return GOOD.slice(0, idx);
265+
}
102266
```
103267

104268
<!-- tabs:end -->

solution/3800-3899/3890.Integers With Multiple Sum of Two Cubes/README_EN.md

Lines changed: 167 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,196 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3890.In
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Preprocessing + Binary Search
76+
77+
We observe that when $a$ or $b$ is greater than $1000$, the expression $a^3 + b^3 > 10^9$. Therefore, we only need to enumerate $1 \leq a \leq b \leq 1000$ and count the occurrences of each integer $x = a^3 + b^3$. Finally, we filter out the integers that appear more than once and sort them in ascending order to obtain all good integers.
78+
79+
We preprocess all good integers and store them in an array $\textit{GOOD}$. For each query, we use binary search to find the index $idx$ of the first integer in $\textit{GOOD}$ that is greater than $n$, then return the first $idx$ integers in $\textit{GOOD}$.
80+
81+
The time complexity is $O(m^2 + k \log k)$, where $m = 1000$ is the enumeration range and $k$ is the number of good integers. The space complexity is $O(k)$.
7682

7783
<!-- tabs:start -->
7884

7985
#### Python3
8086

8187
```python
88+
LIMIT = 10**9
89+
90+
cnt = defaultdict(int)
91+
cubes = [i**3 for i in range(1001)]
8292

93+
for a in range(1, 1001):
94+
for b in range(a, 1001):
95+
x = cubes[a] + cubes[b]
96+
if x > LIMIT:
97+
break
98+
cnt[x] += 1
99+
100+
GOOD = sorted(x for x, v in cnt.items() if v > 1)
101+
102+
103+
class Solution:
104+
def findGoodIntegers(self, n: int) -> list[int]:
105+
idx = bisect_right(GOOD, n)
106+
return GOOD[:idx]
83107
```
84108

85109
#### Java
86110

87111
```java
88-
112+
class Solution {
113+
private static final int LIMIT = (int) 1e9;
114+
private static final List<Integer> GOOD = new ArrayList<>();
115+
116+
static {
117+
Map<Integer, Integer> cnt = new HashMap<>();
118+
int[] cubes = new int[1001];
119+
for (int i = 0; i <= 1000; i++) {
120+
cubes[i] = i * i * i;
121+
}
122+
for (int a = 1; a <= 1000; a++) {
123+
for (int b = a; b <= 1000; b++) {
124+
int x = cubes[a] + cubes[b];
125+
if (x > LIMIT) {
126+
break;
127+
}
128+
cnt.merge(x, 1, Integer::sum);
129+
}
130+
}
131+
for (Map.Entry<Integer, Integer> e : cnt.entrySet()) {
132+
if (e.getValue() > 1) {
133+
GOOD.add(e.getKey());
134+
}
135+
}
136+
137+
Collections.sort(GOOD);
138+
}
139+
140+
public List<Integer> findGoodIntegers(int n) {
141+
int idx = Collections.binarySearch(GOOD, n + 1);
142+
if (idx < 0) {
143+
idx = -idx - 1;
144+
}
145+
return GOOD.subList(0, idx);
146+
}
147+
}
89148
```
90149

91150
#### C++
92151

93152
```cpp
94-
153+
vector<int> GOOD;
154+
155+
auto init = [] {
156+
const int LIMIT = 1e9;
157+
158+
unordered_map<int, int> cnt;
159+
vector<int> cubes(1001);
160+
161+
for (int i = 0; i <= 1000; ++i) {
162+
cubes[i] = i * i * i;
163+
}
164+
165+
for (int a = 1; a <= 1000; ++a) {
166+
for (int b = a; b <= 1000; ++b) {
167+
int x = cubes[a] + cubes[b];
168+
if (x > LIMIT) break;
169+
cnt[x]++;
170+
}
171+
}
172+
173+
for (auto& [x, v] : cnt) {
174+
if (v > 1) {
175+
GOOD.push_back(x);
176+
}
177+
}
178+
179+
sort(GOOD.begin(), GOOD.end());
180+
181+
return 0;
182+
}();
183+
184+
class Solution {
185+
public:
186+
vector<int> findGoodIntegers(int n) {
187+
int idx = upper_bound(GOOD.begin(), GOOD.end(), n) - GOOD.begin();
188+
return vector<int>(GOOD.begin(), GOOD.begin() + idx);
189+
}
190+
};
95191
```
96192
97193
#### Go
98194
99195
```go
196+
var GOOD []int
197+
198+
func init() {
199+
const LIMIT = 1e9
200+
201+
cnt := make(map[int]int)
202+
cubes := make([]int, 1001)
203+
204+
for i := 0; i <= 1000; i++ {
205+
cubes[i] = i * i * i
206+
}
207+
208+
for a := 1; a <= 1000; a++ {
209+
for b := a; b <= 1000; b++ {
210+
x := cubes[a] + cubes[b]
211+
if x > LIMIT {
212+
break
213+
}
214+
cnt[x]++
215+
}
216+
}
217+
218+
for x, v := range cnt {
219+
if v > 1 {
220+
GOOD = append(GOOD, x)
221+
}
222+
}
223+
224+
sort.Ints(GOOD)
225+
}
226+
227+
func findGoodIntegers(n int) []int {
228+
idx := sort.Search(len(GOOD), func(i int) bool {
229+
return GOOD[i] > n
230+
})
231+
return GOOD[:idx]
232+
}
233+
```
234+
235+
#### TypeScript
236+
237+
```ts
238+
const LIMIT = 1e9;
239+
240+
const GOOD: number[] = (() => {
241+
const cnt = new Map<number, number>();
242+
const cubes: number[] = Array.from({ length: 1001 }, (_, i) => i * i * i);
243+
244+
for (let a = 1; a <= 1000; a++) {
245+
for (let b = a; b <= 1000; b++) {
246+
const x = cubes[a] + cubes[b];
247+
if (x > LIMIT) break;
248+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
249+
}
250+
}
251+
252+
const res: number[] = [];
253+
for (const [x, v] of cnt.entries()) {
254+
if (v > 1) res.push(x);
255+
}
256+
257+
res.sort((a, b) => a - b);
258+
return res;
259+
})();
100260

261+
function findGoodIntegers(n: number): number[] {
262+
const idx = _.sortedLastIndex(GOOD, n);
263+
return GOOD.slice(0, idx);
264+
}
101265
```
102266

103267
<!-- tabs:end -->

0 commit comments

Comments
 (0)