/
greedy.rs
45 lines (33 loc) · 1.1 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
42
43
44
45
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
use std::convert::TryInto;
impl Solution {
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
let mut iter = points.iter().map(|p| {
let [x, y]: [_; 2] = p.as_slice().try_into().unwrap();
(x, y)
});
let mut result = 0;
let mut prev = iter.next().unwrap();
for point in iter {
let x_diff = point.0 - prev.0;
let y_diff = point.1 - prev.1;
result += x_diff.abs().max(y_diff.abs());
prev = point;
}
result
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
Self::min_time_to_visit_all_points(points)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}