forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
33 lines (32 loc) · 948 Bytes
/
Solution.java
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
class Solution {
public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
List<Boolean> res = new ArrayList<>();
for (int i = 0; i < l.length; ++i) {
res.add(check(nums, l[i], r[i]));
}
return res;
}
private boolean check(int[] nums, int l, int r) {
if (r - l < 2) {
return true;
}
Set<Integer> s = new HashSet<>();
int mx = Integer.MIN_VALUE;
int mi = Integer.MAX_VALUE;
for (int i = l; i <= r; ++i) {
s.add(nums[i]);
mx = Math.max(mx, nums[i]);
mi = Math.min(mi, nums[i]);
}
if ((mx - mi) % (r - l) != 0) {
return false;
}
int delta = (mx - mi) / (r - l);
for (int i = 1; i <= r - l; ++i) {
if (!s.contains(mi + delta * i)) {
return false;
}
}
return true;
}
}