Skip to content

Commit 94c440f

Browse files
add solution for 1266
1 parent 4d03d5d commit 94c440f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/main/java/com/fishercoder/solutions/_1266.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,22 @@
3131
* */
3232
public class _1266 {
3333
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+
* */
3440
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]));
3750
}
3851
}
3952
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)