20240123 Finished
This commit is contained in:
parent
1c081f7e44
commit
0e5ab485d5
|
@ -25,3 +25,4 @@ mod p2809_minimum_time_to_make_array_sum_at_most_x;
|
||||||
mod p2788_split_strings_by_separator;
|
mod p2788_split_strings_by_separator;
|
||||||
mod p410_split_array_largest_sum;
|
mod p410_split_array_largest_sum;
|
||||||
mod p670_maximum_swap;
|
mod p670_maximum_swap;
|
||||||
|
mod p2765_longest_alternating_subarray;
|
40
src/problem/p2765_longest_alternating_subarray.rs
Normal file
40
src/problem/p2765_longest_alternating_subarray.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/**
|
||||||
|
* [2765] Longest Alternating Subarray
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn alternating_subarray(nums: Vec<i32>) -> i32 {
|
||||||
|
let mut result = -1;
|
||||||
|
|
||||||
|
for i in 0..nums.len() {
|
||||||
|
for j in i + 1..nums.len() {
|
||||||
|
if nums[j] - nums[i] != ((j - i) % 2) as i32 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = result.max((j - i + 1) as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2765() {
|
||||||
|
assert_eq!(Solution::alternating_subarray(vec![2,3,4,3,4]), 4);
|
||||||
|
assert_eq!(Solution::alternating_subarray(vec![4,5,6]), 2);
|
||||||
|
assert_eq!(Solution::alternating_subarray(vec![21,9,5]), -1);
|
||||||
|
assert_eq!(Solution::alternating_subarray(vec![21,22]), 2);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user