36 lines
886 B
Rust
36 lines
886 B
Rust
use std::cmp::max;
|
|
use std::mem::swap;
|
|
|
|
struct Solution;
|
|
impl Solution {
|
|
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
|
|
let mut ans = 0;
|
|
let (mut a,mut b) = (-1,-1);
|
|
let mut temp_ans = 0;
|
|
let mut sub_temp_ans = 0;
|
|
for fruit in fruits {
|
|
if fruit != a && fruit != b {
|
|
ans = max(ans, temp_ans);
|
|
temp_ans = sub_temp_ans + 1;
|
|
sub_temp_ans = 1;
|
|
b = a;
|
|
a = fruit;
|
|
}
|
|
else {
|
|
temp_ans += 1;
|
|
sub_temp_ans += 1;
|
|
if b == fruit {
|
|
swap(&mut a, &mut b);
|
|
sub_temp_ans = 1;
|
|
}
|
|
}
|
|
}
|
|
max(ans, temp_ans)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let sl = Solution::total_fruit(vec![3,3,3,1,2,1,1,2,3,3,4]);
|
|
println!("{:?}", sl);
|
|
}
|