-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path26-random-pick-with-blacklist.rs
57 lines (53 loc) · 1.43 KB
/
26-random-pick-with-blacklist.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
// https://leetcode.cn/problems/random-pick-with-blacklist/
use rand::prelude::*;
struct Solution {
map: std::collections::BTreeMap<i32, i32>,
n: i32,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Solution {
fn new(n: i32, blacklist: Vec<i32>) -> Self {
let mut map = std::collections::BTreeMap::new();
let mut blacklist = blacklist;
blacklist.sort();
if blacklist.is_empty() {
return Self {
map: map,
n: n - blacklist.len() as i32,
};
}
let mut count = 0;
let mut prev = -1;
for i in 0..blacklist.len() {
count += blacklist[i] - prev - 1;
prev = blacklist[i];
map.insert(count, blacklist[i]);
}
Self {
map: map,
n: n - blacklist.len() as i32,
}
}
fn pick(&self) -> i32 {
let mut rng = rand::thread_rng();
let r = rng.gen_range(0, self.n);
let c = self.map.range(..=r).last();
if let Some((&k, &v)) = c {
if r < k {
r
}else{
v + 1 + r - k
}
} else {
r
}
}
}
/**
* Your Solution object will be instantiated and called as such:
* let obj = Solution::new(n, blacklist);
* let ret_1: i32 = obj.pick();
*/