-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2601-prime-subtraction-operation.py
More file actions
36 lines (30 loc) · 968 Bytes
/
2601-prime-subtraction-operation.py
File metadata and controls
36 lines (30 loc) · 968 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
# time complexity: O(n+m*m^0.5)
# space complexity: O(m)
from math import isqrt
from typing import List
class Solution:
def isprime(self, n):
for i in range(2, isqrt(n) + 1):
if n % i == 0:
return False
return True
def primeSubOperation(self, nums: List[int]) -> bool:
maxElement = max(nums)
previousPrime = [0] * (maxElement + 1)
for i in range(2, maxElement + 1):
if self.isprime(i):
previousPrime[i] = i
else:
previousPrime[i] = previousPrime[i-1]
for i in range(len(nums)):
if i == 0:
bound = nums[0]
else:
bound = nums[i] - nums[i-1]
if bound <= 0:
return False
largestPrime = previousPrime[bound - 1]
nums[i] -= largestPrime
return True
nums = [4, 9, 6, 10]
print(Solution().primeSubOperation(nums))