-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpy.py
More file actions
36 lines (32 loc) · 975 Bytes
/
py.py
File metadata and controls
36 lines (32 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import functools
class Solution:
def splitArray(self, nums: list[int]) -> int:
# @functools.cache
# def isprime(n):
# if n <= 1:
# return False
# count = 0
# for i in range(1, n + 1):
# if n % i == 0:
# count += 1
# return count == 2
@functools.cache
def isprime(n: int) -> bool:
if n < 2:
return False
if n == 2 or n == 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n**0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
a = []
b = []
for i in range(len(nums)):
if isprime(i):
a.append(nums[i])
else:
b.append(nums[i])
return abs(sum(a) - sum(b))