Content-Length: 973434 | pFad | http://github.com/Speccy-Rom/leetcode/commit/d64eda26a6cff31ae9e99b4e9b3ef8f6812b536f

1C feat: add solutions to lc problem: No.3618 (#4583) · Speccy-Rom/leetcode@d64eda2 · GitHub
Skip to content

Commit d64eda2

Browse files
yanglbmerom.spiridonov
authored andcommitted
feat: add solutions to lc problem: No.3618 (doocs#4583)
No.3618.Split Array by Prime Indices
1 parent c2a47f5 commit d64eda2

File tree

7 files changed

+364
-8
lines changed

7 files changed

+364
-8
lines changed

solution/3600-3699/3618.Split Array by Prime Indices/README.md

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,153 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3618.Sp
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:埃氏筛 + 模拟
84+
85+
我们可以用埃氏筛法预处理出 $[0, 10^5]$ 范围内的所有质数。然后遍历数组 $
86+
\textit{nums}$,对于 $\textit{nums}[i]$,如果 $i$ 是质数,则将 $\textit{nums}[i]$ 加到答案中,否则将 $-\textit{nums}[i]$ 加到答案中。最后返回答案的绝对值。
87+
88+
忽略预处理的时间和空间,时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度,空间复杂度 $O(1)$。
8489

8590
<!-- tabs:start -->
8691

8792
#### Python3
8893

8994
```python
90-
95+
m = 10**5 + 10
96+
primes = [True] * m
97+
primes[0] = primes[1] = False
98+
for i in range(2, m):
99+
if primes[i]:
100+
for j in range(i + i, m, i):
101+
primes[j] = False
102+
103+
104+
class Solution:
105+
def splitArray(self, nums: List[int]) -> int:
106+
return abs(sum(x if primes[i] else -x for i, x in enumerate(nums)))
91107
```
92108

93109
#### Java
94110

95111
```java
96-
112+
class Solution {
113+
private static final int M = 100000 + 10;
114+
private static boolean[] primes = new boolean[M];
115+
116+
static {
117+
for (int i = 0; i < M; i++) {
118+
primes[i] = true;
119+
}
120+
primes[0] = primes[1] = false;
121+
122+
for (int i = 2; i < M; i++) {
123+
if (primes[i]) {
124+
for (int j = i + i; j < M; j += i) {
125+
primes[j] = false;
126+
}
127+
}
128+
}
129+
}
130+
131+
public long splitArray(int[] nums) {
132+
long ans = 0;
133+
for (int i = 0; i < nums.length; ++i) {
134+
ans += primes[i] ? nums[i] : -nums[i];
135+
}
136+
return Math.abs(ans);
137+
}
138+
}
97139
```
98140

99141
#### C++
100142

101143
```cpp
102-
144+
const int M = 1e5 + 10;
145+
bool primes[M];
146+
auto init = [] {
147+
memset(primes, true, sizeof(primes));
148+
primes[0] = primes[1] = false;
149+
for (int i = 2; i < M; ++i) {
150+
if (primes[i]) {
151+
for (int j = i + i; j < M; j += i) {
152+
primes[j] = false;
153+
}
154+
}
155+
}
156+
return 0;
157+
}();
158+
159+
class Solution {
160+
public:
161+
long long splitArray(vector<int>& nums) {
162+
long long ans = 0;
163+
for (int i = 0; i < nums.size(); ++i) {
164+
ans += primes[i] ? nums[i] : -nums[i];
165+
}
166+
return abs(ans);
167+
}
168+
};
103169
```
104170
105171
#### Go
106172
107173
```go
174+
const M = 100000 + 10
175+
176+
var primes [M]bool
177+
178+
func init() {
179+
for i := 0; i < M; i++ {
180+
primes[i] = true
181+
}
182+
primes[0], primes[1] = false, false
183+
184+
for i := 2; i < M; i++ {
185+
if primes[i] {
186+
for j := i + i; j < M; j += i {
187+
primes[j] = false
188+
}
189+
}
190+
}
191+
}
192+
193+
func splitArray(nums []int) (ans int64) {
194+
for i, num := range nums {
195+
if primes[i] {
196+
ans += int64(num)
197+
} else {
198+
ans -= int64(num)
199+
}
200+
}
201+
return max(ans, -ans)
202+
}
203+
```
108204

205+
#### TypeScript
206+
207+
```ts
208+
const M = 100000 + 10;
209+
const primes: boolean[] = Array(M).fill(true);
210+
211+
const init = (() => {
212+
primes[0] = primes[1] = false;
213+
214+
for (let i = 2; i < M; i++) {
215+
if (primes[i]) {
216+
for (let j = i + i; j < M; j += i) {
217+
primes[j] = false;
218+
}
219+
}
220+
}
221+
})();
222+
223+
function splitArray(nums: number[]): number {
224+
let ans = 0;
225+
for (let i = 0; i < nums.length; i++) {
226+
ans += primes[i] ? nums[i] : -nums[i];
227+
}
228+
return Math.abs(ans);
229+
}
109230
```
110231

111232
<!-- tabs:end -->

solution/3600-3699/3618.Split Array by Prime Indices/README_EN.md

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,152 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3618.Sp
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: Sieve of Eratosthenes + Simulation
82+
83+
We can use the Sieve of Eratosthenes to preprocess all prime numbers in the range $[0, 10^5]$. Then we iterate through the array $\textit{nums}$. For $\textit{nums}[i]$, if $i$ is a prime number, we add $\textit{nums}[i]$ to the answer; otherwise, we add $-\textit{nums}[i]$ to the answer. Finally, we return the absolute value of the answer.
84+
85+
Ignoring the preprocessing time and space, the time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$, and the space complexity is $O(1)$.
8286

8387
<!-- tabs:start -->
8488

8589
#### Python3
8690

8791
```python
88-
92+
m = 10**5 + 10
93+
primes = [True] * m
94+
primes[0] = primes[1] = False
95+
for i in range(2, m):
96+
if primes[i]:
97+
for j in range(i + i, m, i):
98+
primes[j] = False
99+
100+
101+
class Solution:
102+
def splitArray(self, nums: List[int]) -> int:
103+
return abs(sum(x if primes[i] else -x for i, x in enumerate(nums)))
89104
```
90105

91106
#### Java
92107

93108
```java
94-
109+
class Solution {
110+
private static final int M = 100000 + 10;
111+
private static boolean[] primes = new boolean[M];
112+
113+
static {
114+
for (int i = 0; i < M; i++) {
115+
primes[i] = true;
116+
}
117+
primes[0] = primes[1] = false;
118+
119+
for (int i = 2; i < M; i++) {
120+
if (primes[i]) {
121+
for (int j = i + i; j < M; j += i) {
122+
primes[j] = false;
123+
}
124+
}
125+
}
126+
}
127+
128+
public long splitArray(int[] nums) {
129+
long ans = 0;
130+
for (int i = 0; i < nums.length; ++i) {
131+
ans += primes[i] ? nums[i] : -nums[i];
132+
}
133+
return Math.abs(ans);
134+
}
135+
}
95136
```
96137

97138
#### C++
98139

99140
```cpp
100-
141+
const int M = 1e5 + 10;
142+
bool primes[M];
143+
auto init = [] {
144+
memset(primes, true, sizeof(primes));
145+
primes[0] = primes[1] = false;
146+
for (int i = 2; i < M; ++i) {
147+
if (primes[i]) {
148+
for (int j = i + i; j < M; j += i) {
149+
primes[j] = false;
150+
}
151+
}
152+
}
153+
return 0;
154+
}();
155+
156+
class Solution {
157+
public:
158+
long long splitArray(vector<int>& nums) {
159+
long long ans = 0;
160+
for (int i = 0; i < nums.size(); ++i) {
161+
ans += primes[i] ? nums[i] : -nums[i];
162+
}
163+
return abs(ans);
164+
}
165+
};
101166
```
102167
103168
#### Go
104169
105170
```go
171+
const M = 100000 + 10
172+
173+
var primes [M]bool
174+
175+
func init() {
176+
for i := 0; i < M; i++ {
177+
primes[i] = true
178+
}
179+
primes[0], primes[1] = false, false
180+
181+
for i := 2; i < M; i++ {
182+
if primes[i] {
183+
for j := i + i; j < M; j += i {
184+
primes[j] = false
185+
}
186+
}
187+
}
188+
}
189+
190+
func splitArray(nums []int) (ans int64) {
191+
for i, num := range nums {
192+
if primes[i] {
193+
ans += int64(num)
194+
} else {
195+
ans -= int64(num)
196+
}
197+
}
198+
return max(ans, -ans)
199+
}
200+
```
106201

202+
#### TypeScript
203+
204+
```ts
205+
const M = 100000 + 10;
206+
const primes: boolean[] = Array(M).fill(true);
207+
208+
const init = (() => {
209+
primes[0] = primes[1] = false;
210+
211+
for (let i = 2; i < M; i++) {
212+
if (primes[i]) {
213+
for (let j = i + i; j < M; j += i) {
214+
primes[j] = false;
215+
}
216+
}
217+
}
218+
})();
219+
220+
function splitArray(nums: number[]): number {
221+
let ans = 0;
222+
for (let i = 0; i < nums.length; i++) {
223+
ans += primes[i] ? nums[i] : -nums[i];
224+
}
225+
return Math.abs(ans);
226+
}
107227
```
108228

109229
<!-- tabs:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const int M = 1e5 + 10;
2+
bool primes[M];
3+
auto init = [] {
4+
memset(primes, true, sizeof(primes));
5+
primes[0] = primes[1] = false;
6+
for (int i = 2; i < M; ++i) {
7+
if (primes[i]) {
8+
for (int j = i + i; j < M; j += i) {
9+
primes[j] = false;
10+
}
11+
}
12+
}
13+
return 0;
14+
}();
15+
16+
class Solution {
17+
public:
18+
long long splitArray(vector<int>& nums) {
19+
long long ans = 0;
20+
for (int i = 0; i < nums.size(); ++i) {
21+
ans += primes[i] ? nums[i] : -nums[i];
22+
}
23+
return abs(ans);
24+
}
25+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const M = 100000 + 10
2+
3+
var primes [M]bool
4+
5+
func init() {
6+
for i := 0; i < M; i++ {
7+
primes[i] = true
8+
}
9+
primes[0], primes[1] = false, false
10+
11+
for i := 2; i < M; i++ {
12+
if primes[i] {
13+
for j := i + i; j < M; j += i {
14+
primes[j] = false
15+
}
16+
}
17+
}
18+
}
19+
20+
func splitArray(nums []int) (ans int64) {
21+
for i, num := range nums {
22+
if primes[i] {
23+
ans += int64(num)
24+
} else {
25+
ans -= int64(num)
26+
}
27+
}
28+
return max(ans, -ans)
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
private static final int M = 100000 + 10;
3+
private static boolean[] primes = new boolean[M];
4+
5+
static {
6+
for (int i = 0; i < M; i++) {
7+
primes[i] = true;
8+
}
9+
primes[0] = primes[1] = false;
10+
11+
for (int i = 2; i < M; i++) {
12+
if (primes[i]) {
13+
for (int j = i + i; j < M; j += i) {
14+
primes[j] = false;
15+
}
16+
}
17+
}
18+
}
19+
20+
public long splitArray(int[] nums) {
21+
long ans = 0;
22+
for (int i = 0; i < nums.length; ++i) {
23+
ans += primes[i] ? nums[i] : -nums[i];
24+
}
25+
return Math.abs(ans);
26+
}
27+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Speccy-Rom/leetcode/commit/d64eda26a6cff31ae9e99b4e9b3ef8f6812b536f

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy