Skip to content

Commit

Permalink
Create: 0904-fruit-into-baskets
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifhanIlgaz committed Feb 7, 2023
1 parent ee7a3bc commit e535e1b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions rust/0904-fruit-into-baskets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::HashMap;

impl Solution {
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
let mut count = HashMap::new();
let (mut left, mut total, mut res) = (0, 0, 0);

for fruit in &fruits {
*count.entry(*fruit).or_insert(0) += 1;
total += 1;

while count.len() > 2 {
let f = fruits[left];
match count.remove(&f) {
Some(v) if v > 1 => {
count.insert(f, v - 1);
}
_ => {}
}
total -= 1;
left += 1;
}

res = res.max(total);
}

res
}
}

0 comments on commit e535e1b

Please sign in to comment.