Compare commits

...

3 commits

Author SHA1 Message Date
Joe Ardent
cf517aeac3 catching up 2024-05-14 16:34:13 -07:00
Joe Ardent
d2df4afd3d just iterate 2024-05-06 14:35:45 -07:00
Joe Ardent
1c2b0ba376 stolen valor 2024-05-06 14:30:18 -07:00
6 changed files with 99 additions and 0 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

@ -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]

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