Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save D-Lite/25c6c6d04419572e02068bd623be8244 to your computer and use it in GitHub Desktop.

Select an option

Save D-Lite/25c6c6d04419572e02068bd623be8244 to your computer and use it in GitHub Desktop.
Jan 12
Based on hints offered on leetcode, we can just move diagonally as possible before optimising to move vertical or horizontal. But diagonal will reduce the slow rate of movement as much as possible.
Checking the discussions showed a mathematical formula: time = max(|x1 - x2|, |y1-y2|)
```
use std::cmp::max;
impl Solution {
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
if points.len() <= 1 {
return 0;
}
let mut ans: i32 = 0;
for i in 0..points.len() - 1 {
let (curr_x, curr_y) = (points[i][0], points[i][1]);
let (next_x, next_y) = (points[i + 1][0], points[i + 1][1]);
let dx = (next_x - curr_x).abs();
let dy = (next_y - curr_y).abs();
ans += max(dx, dy);
}
ans
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment