-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0575_distribute_candies.rs
More file actions
41 lines (37 loc) · 970 Bytes
/
s0575_distribute_candies.rs
File metadata and controls
41 lines (37 loc) · 970 Bytes
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
#![allow(unused)]
pub struct Solution {}
// microsoft interview
impl Solution {
pub fn distribute_candies(candies: Vec<i32>) -> i32 {
use std::collections::HashSet;
// initial hashset filtered duplicate values;
let (mut set, len) = (
HashSet::new(),
if candies.len() % 2 == 0 {
candies.len() / 2
} else {
(candies.len() + 1) / 2
},
);
for i in 0..candies.len() {
if set.contains(&candies[i]) {
continue;
}
// sister can get max half of candies
if set.len() == len {
break;
}
// sister get one candy
set.insert(candies[i]);
}
set.len() as i32
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_575() {
assert_eq!(Solution::distribute_candies(vec![1, 1, 2, 2, 3, 3]), 3);
}
}