using the windows() iter methods is slow

This commit is contained in:
Joe Ardent 2024-04-30 08:34:17 -07:00
parent e8b350b64b
commit 8bf70ff701
3 changed files with 38 additions and 3 deletions

View file

@ -13,9 +13,7 @@ fn foo(nums: &mut [i32], k: i32) -> i32 {
let mut right = nums.len() - 1; let mut right = nums.len() - 1;
while left < right { while left < right {
let lv = nums[left]; let sum = nums[left] + nums[right];
let rv = nums[right];
let sum = lv + rv;
if sum < k { if sum < k {
left += 1; left += 1;
} else if sum > k { } else if sum > k {

View file

@ -0,0 +1,8 @@
[package]
name = "max_vowels_in_substring_k"
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,29 @@
fn main() {
//dbg!(m("aennnnnnnnnnnnniii".to_string(), 4));
//dbg!(m("abciiidef".to_string(), 3));
dbg!(m("leetcode".to_string(), 3));
}
fn m(s: String, k: i32) -> i32 {
let mut cur: i32 = s.chars().take(k as usize).map(is_vowel).sum();
let cur = &mut cur;
let mut max = *cur;
let max = &mut max;
let add = s.chars().skip(k as usize);
let mut sub = s.chars();
for c in add {
let s = sub.next().unwrap();
*cur -= is_vowel(s);
*cur += is_vowel(c);
*max = *max.max(cur);
}
*max
}
fn is_vowel(c: char) -> i32 {
match c {
'e' | 'i' | 'o' | 'u' | 'a' => 1,
_ => 0,
}
}