Compare commits
3 commits
8bf70ff701
...
cf517aeac3
Author | SHA1 | Date | |
---|---|---|---|
|
cf517aeac3 | ||
|
d2df4afd3d | ||
|
1c2b0ba376 |
6 changed files with 99 additions and 0 deletions
6
find_pivot_index/Cargo.toml
Normal file
6
find_pivot_index/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "find_pivot_index"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
24
find_pivot_index/src/main.rs
Normal file
24
find_pivot_index/src/main.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
fn f(nums: Vec<i32>) -> i32 {
|
||||
let mut pivot = -1;
|
||||
let mut rights = Vec::with_capacity(nums.len());
|
||||
let mut cur = 0;
|
||||
for n in nums.iter().rev() {
|
||||
cur += n;
|
||||
rights.push(cur);
|
||||
}
|
||||
rights.reverse();
|
||||
let rights = &rights;
|
||||
cur = 0;
|
||||
for (i, num) in nums.into_iter().enumerate() {
|
||||
cur += num;
|
||||
if cur == rights[i] {
|
||||
pivot = i as i32;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pivot
|
||||
}
|
6
longest_ones_after_deleting_one_element/Cargo.toml
Normal file
6
longest_ones_after_deleting_one_element/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "longest_ones_after_deleting_one_element"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
27
longest_ones_after_deleting_one_element/src/main.rs
Normal file
27
longest_ones_after_deleting_one_element/src/main.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
fn main() {
|
||||
dbg!(l(vec![1, 1, 0, 1]));
|
||||
dbg!(l(vec![0, 1, 1, 1, 0, 1, 1, 0, 1]));
|
||||
}
|
||||
|
||||
fn l(nums: Vec<i32>) -> i32 {
|
||||
let nums = &nums;
|
||||
let len = nums.len();
|
||||
let mut max = 0;
|
||||
let mut start = 0;
|
||||
let mut zs = 0;
|
||||
|
||||
for (i, &n) in nums.iter().enumerate() {
|
||||
if n == 0 {
|
||||
zs += 1;
|
||||
}
|
||||
if zs > 1 {
|
||||
if nums[start as usize] == 0 {
|
||||
zs -= 1;
|
||||
}
|
||||
start += 1;
|
||||
}
|
||||
max = max.max(i as i32 - start);
|
||||
}
|
||||
|
||||
max.min(len as i32 - 1)
|
||||
}
|
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]
|
28
longest_ones_with_k_flips/src/main.rs
Normal file
28
longest_ones_with_k_flips/src/main.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
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
|
||||
}
|
Loading…
Reference in a new issue