-
Notifications
You must be signed in to change notification settings - Fork 3
/
1266-minimum-time-visiting-all-points.js
112 lines (93 loc) · 2.91 KB
/
1266-minimum-time-visiting-all-points.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 1266. Minimum Time Visiting All Points
// https://leetcode.com/problems/minimum-time-visiting-all-points/
/*
On a plane there are n points with integer coordinates points[i] = [xi, yi].
Your task is to find the minimum time in seconds to visit all points.
You can move according to the next rules:
In one second always you can either move vertically, horizontally by one unit or
diagonally (it means to move one unit vertically and one unit horizontally in
one second). You have to visit the points in the same order as they appear in
the array.
*/
import { strictEqual } from 'assert';
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 64 ms, faster than 50.00% of JavaScript online submissions
// Memory Usage: 35.1 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number[][]} points
// * @return {number}
// */
// const minTimeToVisitAllPoints = points =>
// points.reduce(
// (acc, [x, y], idx) =>
// points.length - 1 === idx
// ? acc
// : acc +
// Math.max(
// Math.abs(x - points[idx + 1][0]),
// Math.abs(y - points[idx + 1][1]),
// ),
// 0,
// );
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 56 ms, faster than 87.31% of JavaScript online submissions
// Memory Usage: 35 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number[][]} points
// * @return {number}
// */
// const minTimeToVisitAllPoints = points =>
// points
// .slice(0, points.length - 1)
// .reduce(
// (acc, [x, y], idx) =>
// acc +
// Math.max(
// Math.abs(x - points[idx + 1][0]),
// Math.abs(y - points[idx + 1][1]),
// ),
// 0,
// );
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 56 ms, faster than 86.93% of JavaScript online submissions
// Memory Usage: 35 MB, less than 100.00% of JavaScript online submissions
/**
* @param {number[][]} points
* @return {number}
*/
const minTimeToVisitAllPoints = points =>
points.reduce(
(acc, [x, y], idx) =>
idx === points.length - 1
? acc
: acc +
Math.max(
Math.abs(x - points[idx + 1][0]),
Math.abs(y - points[idx + 1][1]),
),
0,
);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Example 1:
strictEqual(
minTimeToVisitAllPoints([
[1, 1],
[3, 4],
[-1, 0],
]),
7,
);
// Explanation: One optimal path is
// [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
// Time from [1,1] to [3,4] = 3 seconds
// Time from [3,4] to [-1,0] = 4 seconds
// Total time = 7 seconds
// Example 2:
strictEqual(
minTimeToVisitAllPoints([
[3, 2],
[-2, 2],
]),
5,
);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=