dumb_shit/longest_ones_with_k_flips/src/main.rs

31 lines
607 B
Rust
Raw Normal View History

2024-05-06 21:30:18 +00:00
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;
2024-05-06 21:35:45 +00:00
for i in 0..len {
end = i;
2024-05-06 21:30:18 +00:00
if nums[end as usize] == 0 {
k -= 1;
}
if k < 0 {
start += 1;
if nums[start as usize] == 0 {
k += 1;
}
}
}
end - start
}