catching up

This commit is contained in:
Joe Ardent 2024-05-14 16:34:13 -07:00
parent d2df4afd3d
commit cf517aeac3
5 changed files with 66 additions and 5 deletions

View file

@ -0,0 +1,6 @@
[package]
name = "find_pivot_index"
version = "0.1.0"
edition = "2021"
[dependencies]

View 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
}

View file

@ -0,0 +1,6 @@
[package]
name = "longest_ones_after_deleting_one_element"
version = "0.1.0"
edition = "2021"
[dependencies]

View 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)
}

View file

@ -10,12 +10,10 @@ 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;
for i in 0..len {
end = i;
if nums[end as usize] == 0 {
for i in 0..len as usize {
if nums[i] == 0 {
k -= 1;
}
@ -26,5 +24,5 @@ fn l(nums: Vec<i32>, k: i32) -> i32 {
}
}
}
end - start
len - start - 1
}