31 lines
612 B
Rust
31 lines
612 B
Rust
|
fn main() {
|
||
|
//dbg!(l(vec![1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 2));
|
||
|
dbg!(l(
|
||
|
vec![0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1],
|
||
|
3
|
||
|
));
|
||
|
}
|
||
|
|
||
|
fn l(nums: Vec<i32>, k: i32) -> i32 {
|
||
|
let len = nums.len() as i32;
|
||
|
let mut k = k;
|
||
|
let mut start: i32 = -1;
|
||
|
let mut end: i32 = -1;
|
||
|
let nums = &nums;
|
||
|
|
||
|
while end < len - 1 {
|
||
|
end += 1;
|
||
|
if nums[end as usize] == 0 {
|
||
|
k -= 1;
|
||
|
}
|
||
|
|
||
|
if k < 0 {
|
||
|
start += 1;
|
||
|
if nums[start as usize] == 0 {
|
||
|
k += 1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
end - start
|
||
|
}
|