File tree Expand file tree Collapse file tree 2 files changed +52
-2
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 31
31
* */
32
32
public class _1266 {
33
33
public static class Solution1 {
34
+ /**
35
+ * Time: O(n)
36
+ * Space: O(1)
37
+ *
38
+ * credit: https://leetcode.com/problems/minimum-time-visiting-all-points/discuss/436142/Sum-of-Chebyshev-distance-between-two-consecutive-points
39
+ * */
34
40
public int minTimeToVisitAllPoints (int [][] points ) {
35
- //TODO: implement it
36
- return -1 ;
41
+ int minTime = 0 ;
42
+ for (int i = 0 ; i < points .length - 1 ; i ++) {
43
+ minTime += chebyshevDistance (points [i ], points [i +1 ]);
44
+ }
45
+ return minTime ;
46
+ }
47
+
48
+ private int chebyshevDistance (int [] pointA , int [] pointB ) {
49
+ return Math .max (Math .abs (pointA [0 ] - pointB [0 ]), Math .abs (pointA [1 ] - pointB [1 ]));
37
50
}
38
51
}
39
52
}
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._1266 ;
4
+ import org .junit .BeforeClass ;
5
+ import org .junit .Test ;
6
+
7
+ import static org .junit .Assert .assertEquals ;
8
+
9
+ public class _1266Test {
10
+ private static _1266 .Solution1 solution1 ;
11
+ private static int [][] points ;
12
+
13
+ @ BeforeClass
14
+ public static void setup () {
15
+ solution1 = new _1266 .Solution1 ();
16
+ }
17
+
18
+ @ Test
19
+ public void test1 () {
20
+ points = new int [][]{
21
+ {1 , 1 },
22
+ {3 , 4 },
23
+ {-1 , 0 }
24
+ };
25
+ assertEquals (7 , solution1 .minTimeToVisitAllPoints (points ));
26
+ }
27
+
28
+ @ Test
29
+ public void test2 () {
30
+ points = new int [][]{
31
+ {3 , 2 },
32
+ {-2 , 2 }
33
+ };
34
+ assertEquals (5 , solution1 .minTimeToVisitAllPoints (points ));
35
+ }
36
+
37
+ }
You can’t perform that action at this time.
0 commit comments