Functional programming:
using static System.Math;
public class Solution {
public int MinTimeToVisitAllPoints(int[][] points) =>
points.Skip(1).Aggregate(
new { count = 0, source = points.First() },
(stat, dest) =>
new
{
count = stat.count +
Max(Abs(dest[0] - stat.source[0]), Abs(dest[1] - stat.source[1])),
source = dest
}
).count;
}
Imperative programming:
using static System.Math;
public class Solution {
public int MinTimeToVisitAllPoints(int[][] points) {
var count = 0;
var source = points.First();
foreach (var dest in points.Skip(1)) {
var lengthX = dest[0] - source[0];
var lengthY = dest[1] - source[1];
count += Max(Abs(lengthX), Abs(lengthY));
source = dest;
}
return count;
}
}
Source:
https://leetcode.com/problems/minimum-time-visiting-all-points/
No comments:
Post a Comment