stolen valor
This commit is contained in:
parent
8bf70ff701
commit
1c2b0ba376
2 changed files with 38 additions and 0 deletions
8
longest_ones_with_k_flips/Cargo.toml
Normal file
8
longest_ones_with_k_flips/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "longest_ones_with_k_flips"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
30
longest_ones_with_k_flips/src/main.rs
Normal file
30
longest_ones_with_k_flips/src/main.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
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
|
||||
}
|
Loading…
Reference in a new issue