-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.rs
40 lines (36 loc) · 984 Bytes
/
Solution.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
use rand::{thread_rng, Rng};
struct Solution {
sum: Vec<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(w: Vec<i32>) -> Self {
let n = w.len();
let mut sum = vec![0; n + 1];
for i in 1..=n {
sum[i] = sum[i - 1] + w[i - 1];
}
Self { sum }
}
fn pick_index(&self) -> i32 {
let x = thread_rng().gen_range(1, self.sum.last().unwrap() + 1);
let (mut left, mut right) = (1, self.sum.len() - 1);
while left < right {
let mid = (left + right) >> 1;
if self.sum[mid] < x {
left = mid + 1;
} else {
right = mid;
}
}
(left - 1) as i32
}
}
/**
* Your Solution object will be instantiated and called as such:
* let obj = Solution::new(w);
* let ret_1: i32 = obj.pick_index();
*/