-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinStepsSequenceOfPoints.java
53 lines (42 loc) · 1.03 KB
/
MinStepsSequenceOfPoints.java
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
/* daily coding problem #100
* https://www.geeksforgeeks.org/minimum-steps-needed-to-cover-a-sequence-of-points-on-an-infinite-grid/
*/
// turns out that min steps between 2 points
// (go diagonal first then vertical/horizontal
// is the Max of (abs(x2-x1), abs(y2-y1))
import java.util.*;
public class MinStepsSequenceOfPoints
{
public static class Point
{
int x;
int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
public static void main(String[] args)
{
Point p1 = new Point(4,6);
Point p2 = new Point(1,2);
Point p3 = new Point(4,5);
Point p4 = new Point(10,12);
List<Point> list = new ArrayList<>(Arrays.asList(p1,p2,p3,p4));
int minSteps = calcMinSteps(list);
System.out.println(minSteps);
}
public static int calcMinSteps(List<Point> list)
{
int minSteps = 0;
for(int i = 1; i < list.size(); i++)
{
Point p1 = list.get(i-1);
Point p2 = list.get(i);
int steps = Math.max(Math.abs(p1.x - p2.x), Math.abs(p1.y-p2.y));
minSteps += steps;
}
return minSteps;
}
}