Arithmetic Subarrays LeetCode solution in Rust

1
impl Solution {
2
pub fn check_arithmetic_subarrays(nums: Vec<i32>, l: Vec<i32>, r: Vec<i32>) -> Vec<bool> {
3
let mut ans = Vec::new();
4

5
for i in 0..l.len() {
6
let left = l[i] as usize;
7
let (min, max) = nums[left..=r[i] as usize]
8
.iter()
9
.fold((i32::MAX, i32::MIN), |acc, x| {
10
(acc.0.min(*x), acc.1.max(*x))
11
});
12
if max == min {
13
ans.push(true);
14
continue;
15
};
16

17
let N = r[i] - l[i];
18
let d = (max - min) / N;
19
if (max - min) % N != 0 {
20
ans.push(false);
21
continue;
22
};
23

24
let mut visited = vec![false; N as usize + 1];
25
if (0..=N as usize).all(|j| {
26
if (nums[left + j] - min) % d != 0 || visited[((nums[left + j] - min) / d) as usize] {
27
false
28
} else {
29
visited[((nums[left + j] - min) / d) as usize] = true;
30
true
31
}
32
}) {
33
ans.push(true)
34
} else {
35
ans.push(false)
36
};
37
}
38

39
ans
40
}
41
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0