-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-distinct-averages.rs
52 lines (45 loc) · 1.18 KB
/
number-of-distinct-averages.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
struct Solution;
#[derive(Hash, PartialEq, Eq)]
struct Float {
a: i32,
b: i32,
}
impl Solution {
pub fn distinct_averages(nums: Vec<i32>) -> i32 {
let mut nums = nums;
nums.sort();
use std::collections::HashSet;
let mut set: HashSet<Float> = HashSet::new();
let (mut i, mut j) = (0, nums.len() - 1);
while i < j {
set.insert(Float {
a: (nums[i] + nums[j]) / 2,
b: (nums[i] + nums[j]) % 2,
});
i += 1;
j -= 1;
}
set.len() as i32
}
}
fn main() {
println!(
"{}",
Solution::distinct_averages(vec![9, 5, 7, 8, 7, 9, 8, 2, 0, 7])
);
println!("{}", Solution::distinct_averages(vec![4, 1, 4, 0, 3, 5]));
println!("{}", Solution::distinct_averages(vec![1, 100]));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert_eq!(
Solution::distinct_averages(vec![9, 5, 7, 8, 7, 9, 8, 2, 0, 7]),
5
);
assert_eq!(Solution::distinct_averages(vec![4, 1, 4, 0, 3, 5]), 2);
assert_eq!(Solution::distinct_averages(vec![1, 100]), 1);
}
}