-
Notifications
You must be signed in to change notification settings - Fork 3
/
2614.cpp
35 lines (34 loc) · 1.03 KB
/
2614.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
class Solution {
public:
bool isPrime(int num, vector<int>& primes) {
if (num == 1) return false;
for (auto& prime : primes) {
if (num == prime) return true;
if (num % prime == 0) return false;
}
return true;
}
int diagonalPrime(vector<vector<int>>& nums) {
unordered_set<int> notPrime;
vector<int> primes;
notPrime.insert(1);
for (int i = 2; i <= 2 * 1e3; ++i) {
if (notPrime.count(i)) continue;
primes.push_back(i);
for (int j = 2; i * j <= 2 * 1e3; ++j) {
notPrime.insert(i * j);
}
}
int n = nums.size();
int res = 0;
for (int i = 0; i < n; ++i) {
if (!isPrime(nums[i][i], primes)) continue;
res = max(res, nums[i][i]);
}
for (int i = 0; i < n; ++i) {
if (!isPrime(nums[i][n - i - 1], primes)) continue;
res = max(res, nums[i][n - i - 1]);
}
return res;
}
};