dumb_shit/longest_ones_with_k_flips/src/main.rs
2024-05-14 16:34:13 -07:00

28 lines
565 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 nums = &nums;
for i in 0..len as usize {
if nums[i] == 0 {
k -= 1;
}
if k < 0 {
start += 1;
if nums[start as usize] == 0 {
k += 1;
}
}
}
len - start - 1
}