Skip to content

Commit e535e1b

Browse files
committed
Create: 0904-fruit-into-baskets
1 parent ee7a3bc commit e535e1b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: rust/0904-fruit-into-baskets.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
5+
let mut count = HashMap::new();
6+
let (mut left, mut total, mut res) = (0, 0, 0);
7+
8+
for fruit in &fruits {
9+
*count.entry(*fruit).or_insert(0) += 1;
10+
total += 1;
11+
12+
while count.len() > 2 {
13+
let f = fruits[left];
14+
match count.remove(&f) {
15+
Some(v) if v > 1 => {
16+
count.insert(f, v - 1);
17+
}
18+
_ => {}
19+
}
20+
total -= 1;
21+
left += 1;
22+
}
23+
24+
res = res.max(total);
25+
}
26+
27+
res
28+
}
29+
}

0 commit comments

Comments
 (0)