Minimum time to visit all points
Solution
- Just loop through each point, and time taken from one point to another is just the maximum of dx and dy of two points
| Time | Space | Explanation |
|---|
O() | O() | |
function minTimeToVisitAllPoints(points: number[][]): number {
let timeTaken = 0;
for(let i = 0; i < points.length - 1; i++) {
const [x1, y1] = points[i];
const [x2, y2] = points[i + 1];
const dx = Math.abs(x2 - x1)
const dy = Math.abs(y2 - y1)
timeTaken += Math.max(dx, dy);
}
return timeTaken;
};