20241107 finished.

This commit is contained in:
jackfiled 2024-11-07 20:20:34 +08:00
parent bbb8aec61e
commit 9725e1b05b
2 changed files with 62 additions and 0 deletions

View File

@ -300,3 +300,5 @@ mod p633_sum_of_square_numbers;
mod p3222_find_the_winning_player_in_coin_game;
mod p3254_find_the_power_of_k_size_subarrays_i;
mod p3255_find_the_power_of_k_size_subarrays_ii;

View File

@ -0,0 +1,60 @@
/**
* [3255] Find the Power of K-Size Subarrays II
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn results_array(nums: Vec<i32>, k: i32) -> Vec<i32> {
let k = k as usize;
let n = nums.len();
let mut count = 0;
for i in 0..(k - 1) {
if nums[i] + 1 == nums[i + 1] {
count += 1;
}
}
let mut result = Vec::with_capacity(n - k + 1);
result.push(if count == k - 1 { nums[k - 1] } else { -1 });
for i in k..n {
if nums[i - 1] + 1 == nums[i] {
count += 1;
}
if nums[i - k] + 1 == nums[i - k + 1] {
count -= 1;
}
result.push(if count == k - 1 { nums[i] } else { -1 });
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3255() {
assert_eq!(
vec![3, 4, -1, -1, -1],
Solution::results_array(vec![1, 2, 3, 4, 3, 2, 5], 3)
);
assert_eq!(
vec![-1, -1],
Solution::results_array(vec![2, 2, 2, 2, 2], 4)
);
assert_eq!(
vec![-1, 3, -1, 3, -1],
Solution::results_array(vec![3, 2, 3, 2, 3, 2], 2)
);
}
}