-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReach.java
More file actions
84 lines (73 loc) · 2.45 KB
/
Copy pathReach.java
File metadata and controls
84 lines (73 loc) · 2.45 KB
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
/*
* You are in an infinite 2D grid where you can move in any of the 8 directions :
*
(x,y) to
(x+1, y),
(x - 1, y),
(x, y+1),
(x, y-1),
(x-1, y-1),
(x+1,y+1),
(x-1,y+1),
(x+1,y-1)
You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first point.
Example :
Input : [(0, 0), (1, 1), (1, 2)]
Output : 2
It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2).
This question is intentionally left slightly vague. Clarify the question by trying out a few cases in the “See Expected Output” section.
*/
import java.util.*;
public class Reach{
public static int coverPoints(ArrayList<Integer> X, ArrayList<Integer> Y) {
if(X == null || X.size() == 0||
Y == null || Y.size() == 0||
X.size() != Y.size())
return 0;
int result = 0;
for(int i = 0; i < X.size()-1; i++){
if(Math.abs(X.get(i+1)- X.get(i)) == 1 &&
Math.abs(Y.get(i+1) - Y.get(i)) == 1){
result++;
continue;
}
if(X.get(i+1) == X.get(i)){
if(Math.abs(Y.get(i+1) - Y.get(i)) == 1){
result++;
continue;
}
}
if(Y.get(i+1) == Y.get(i)){
if(Math.abs(X.get(i+1) - X.get(i)) == 1){
result++;
continue;
}
}
}
return result;
}
public static int coverPointsEfficient(ArrayList<Integer> X, ArrayList<Integer> Y) {
if(X == null || X.size() == 0||
Y == null || Y.size() == 0||
X.size() != Y.size())
return 0;
int result = 0;
for(int i = 0; i < X.size()-1; i++){
int x = Math.abs(X.get(i) - X.get(i + 1));
int y = Math.abs(Y.get(i) - Y.get(i + 1));
result += Math.max(x, y);
}
return result;
}
public static void main(String[] args){
ArrayList<Integer> X = new ArrayList<Integer>();
ArrayList<Integer> Y = new ArrayList<Integer>();
X.add(-7);
X.add(1);
//X.add(1);
Y.add(-13);
//Y.add(1);
Y.add(-5);
System.out.println(coverPointsEfficient(X, Y));
}
}