forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mex.rs
79 lines (77 loc) · 2.21 KB
/
mex.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::collections::BTreeSet;
// Find minimum excluded number from a set of given numbers using a set
/// Finds the MEX of the values provided in `arr`
/// Uses [`BTreeSet`](std::collections::BTreeSet)
/// O(nlog(n)) implementation
pub fn mex_using_set(arr: &[i64]) -> i64 {
let mut s: BTreeSet<i64> = BTreeSet::new();
for i in 0..arr.len() + 1 {
s.insert(i as i64);
}
for x in arr {
s.remove(x);
}
// TODO: change the next 10 lines to *s.first().unwrap() when merged into stable
// set should never have 0 elements
if let Some(x) = s.into_iter().next() {
x
} else {
panic!("Some unknown error in mex_using_set")
}
}
/// Finds the MEX of the values provided in `arr`
/// Uses sorting
/// O(nlog(n)) implementation
pub fn mex_using_sort(arr: &[i64]) -> i64 {
let mut arr = arr.to_vec();
arr.sort();
let mut mex = 0;
for x in arr {
if x == mex {
mex += 1;
}
}
mex
}
#[cfg(test)]
mod tests {
use super::*;
struct MexTests {
test_arrays: Vec<Vec<i64>>,
outputs: Vec<i64>,
}
impl MexTests {
fn new() -> Self {
return Self {
test_arrays: vec![
vec![-1, 0, 1, 2, 3],
vec![-100, 0, 1, 2, 3, 5],
vec![-1000000, 0, 1, 2, 5],
vec![2, 0, 1, 2, 4],
vec![1, 2, 3, 0, 4],
vec![0, 1, 5, 2, 4, 3],
vec![0, 1, 2, 3, 4, 5, 6],
vec![0, 1, 2, 3, 4, 5, 6, 7],
vec![0, 1, 2, 3, 4, 5, 6, 7, 8],
],
outputs: vec![4, 4, 3, 3, 5, 6, 7, 8, 9],
};
}
fn test_function(&self, f: fn(&[i64]) -> i64) {
for (nums, output) in self.test_arrays.iter().zip(self.outputs.iter()) {
assert_eq!(f(nums), *output);
}
}
}
#[test]
fn test_mex_using_set() {
let tests = MexTests::new();
mex_using_set(&[1, 23, 3]);
tests.test_function(mex_using_set);
}
#[test]
fn test_mex_using_sort() {
let tests = MexTests::new();
tests.test_function(mex_using_sort);
}
}