/
greedy.rs
41 lines (31 loc) · 1.13 KB
/
greedy.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
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
use std::convert::TryInto;
impl Solution {
pub fn average_waiting_time(customers: Vec<Vec<i32>>) -> f64 {
let mut total_waiting_time = 0;
let mut free_time = 0;
let n = customers.len();
for customer in customers {
let [arrival, time]: [_; 2] = customer.try_into().ok().unwrap();
free_time = free_time.max(arrival) + time;
total_waiting_time += i64::from(free_time - arrival);
}
#[allow(clippy::cast_precision_loss)] // Expected.
let result = (total_waiting_time as f64) / (n as f64);
result
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn average_waiting_time(customers: Vec<Vec<i32>>) -> f64 {
Self::average_waiting_time(customers)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}