forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
30 lines (30 loc) · 933 Bytes
/
Solution.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
class Solution {
public:
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
vector<bool> ans;
auto check = [](vector<int>& nums, int l, int r) {
unordered_set<int> s;
int n = r - l + 1;
int a1 = 1 << 30, an = -a1;
for (int i = l; i <= r; ++i) {
s.insert(nums[i]);
a1 = min(a1, nums[i]);
an = max(an, nums[i]);
}
if ((an - a1) % (n - 1)) {
return false;
}
int d = (an - a1) / (n - 1);
for (int i = 1; i < n; ++i) {
if (!s.count(a1 + (i - 1) * d)) {
return false;
}
}
return true;
};
for (int i = 0; i < l.size(); ++i) {
ans.push_back(check(nums, l[i], r[i]));
}
return ans;
}
};