-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEscapeTheGhost.java
More file actions
25 lines (25 loc) · 858 Bytes
/
Copy pathEscapeTheGhost.java
File metadata and controls
25 lines (25 loc) · 858 Bytes
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
class Solution {
static boolean oppositeSigns(int x, int y)
{
return ((x ^ y) < 0);
}
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int jumps = Math.abs(target[0])+Math.abs(target[1]);
for(int i=0;i<ghosts.length;i++){
int lgjumps=0,rgjumps=0;
if(oppositeSigns(target[0],ghosts[i][0])){
lgjumps = Math.abs(target[0])+Math.abs(ghosts[i][0]);
}else{
lgjumps = Math.abs(ghosts[i][0] -target[0]);
}
if(oppositeSigns(target[0],ghosts[i][0])){
rgjumps = Math.abs(target[1])+Math.abs(ghosts[i][1]);
}else{
rgjumps = Math.abs(ghosts[i][1] -target[1]);
}
if((rgjumps + lgjumps)<=jumps)
return false;
}
return true;
}
}