Compare commits
No commits in common. "cf517aeac3718ee5d42aaa31568646d3437bbe3a" and "8bf70ff701c755ece0c228058a333f4fb053c21d" have entirely different histories.
cf517aeac3
...
8bf70ff701
6 changed files with 0 additions and 99 deletions
|
@ -1,6 +0,0 @@
|
|||
[package]
|
||||
name = "find_pivot_index"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
|
@ -1,24 +0,0 @@
|
|||
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
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
[package]
|
||||
name = "longest_ones_after_deleting_one_element"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
|
@ -1,27 +0,0 @@
|
|||
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)
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
[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]
|
|
@ -1,28 +0,0 @@
|
|||
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