-
Notifications
You must be signed in to change notification settings - Fork 3
/
2601.cpp
37 lines (36 loc) · 1.16 KB
/
2601.cpp
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
37
class Solution {
public:
bool primeSubOperation(vector<int>& nums) {
vector<bool> primeTable(1001, true);
primeTable[0] = false;
primeTable[1] = false;
for (int i = 1; i <= 1000; ++i) {
if (primeTable[i] == false) continue;
for (int num = i * 2; num <= 1000; num += i) {
primeTable[num] = false;
}
}
vector<int> primes;
for (int i = 0; i <= 1000; ++i) {
if (primeTable[i]) {
primes.push_back(i);
}
}
int n = nums.size();
for (int i = n - 2; i >= 0; --i) {
int next = nums[i + 1];
int current = nums[i];
if (current < next) continue;
// current - ? < next
// ? > current - next
int target = current - next;
auto it = upper_bound(primes.begin(), primes.end(), target);
if (it == primes.end()) return false;
int index = it - primes.begin();
int prime = primes[index];
if (nums[i] <= prime) return false;
nums[i] -= prime;
}
return true;
}
};