rust_test/src/main.rs

45 lines
1.1 KiB
Rust
Raw Normal View History

use std::cmp::max;
2025-07-22 21:59:38 +08:00
use std::collections::HashMap;
struct Solution;
impl Solution {
2025-07-22 21:59:38 +08:00
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
let mut map: HashMap<i32, usize> = HashMap::new();
2025-07-11 09:26:00 +08:00
let mut ans = 0;
2025-07-22 21:59:38 +08:00
for i in 0..nums.len() {
if map.contains_key(&nums[i]) {
let l = i - map.len();
let r = i;
let mut temp = 0;
let mut j = l;
while j < r {
temp += nums[j];
j+=1;
}
ans = max(ans, temp);
j = l;
while nums[j] != nums[i] {
map.remove(&nums[j]);
j += 1;
}
map.insert(nums[i], i);
}
else {
map.insert(nums[i], i);
}
}
2025-07-22 21:59:38 +08:00
let mut temp = 0;
let mut j = nums.len() - map.len();
while j < nums.len() {
temp += nums[j];
j += 1;
}
2025-07-22 21:59:38 +08:00
max(ans, temp)
}
}
fn main() {
2025-07-22 21:59:38 +08:00
let sl = Solution::maximum_unique_subarray(vec![4,2,4,5,6]);
println!("{}", sl);
}