-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
61 lines (51 loc) · 1.74 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
fn main() {
Solution::subarrays_with_k_distinct(vec![1,2,1,2,3], 2);
}
struct Solution {}
impl Solution {
pub fn subarrays_with_k_distinct(a: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
use std::collections::HashMap;
let mut map: HashMap<i32, i32> = HashMap::new();
let (mut start, mut end, mut res) = (0, 0, 0);
while end < a.len() {
map.entry(a[end]).and_modify(|x| *x += 1).or_insert(1);
end += 1;
while map.len() > k { // if too long, shrink to k
map.entry(a[start]).and_modify(|x| *x -= 1);
if map.get(&a[start]).unwrap() == &0 {
map.remove(&a[start]);
}
start += 1;
}
if map.len() == k {
let mut tmap = map.clone();
let mut inc = 0;
while tmap.len() == k {
tmap.entry(a[start + inc]).and_modify(|x| *x -= 1);
if tmap.get(&a[start + inc]).unwrap() == &0 {
tmap.remove(&a[start + inc]);
}
inc += 1;
}
res += inc;
}
}
res as i32
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::subarrays_with_k_distinct(vec![1,2,1,2,3], 2), 7);
assert_eq!(Solution::subarrays_with_k_distinct(vec![1,2,1,3,4], 3), 3);
}
#[test]
fn edge() {
assert_eq!(Solution::subarrays_with_k_distinct(vec![1,2,1,2,3], 3), 3);
assert_eq!(Solution::subarrays_with_k_distinct(vec![1], 3), 0);
assert_eq!(Solution::subarrays_with_k_distinct(vec![], 3), 0);
}
}