Skip to content

Commit 1d10f51

Browse files
authored
Check if one can intercept at target
1 parent afbf5db commit 1d10f51

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Escape The Ghosts.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public boolean escapeGhosts(int[][] ghosts, int[] target) {
3+
// explained as triangle inequality
4+
// if ghost can reach target as fast as pacman,
5+
// then it must can intercept pacman in the middle or at the end
6+
// Just calculate if one path of ghost is <= path of pacman; Lost if so
7+
int path = Math.abs(target[0]) + Math.abs(target[1]);
8+
for(int[] ghost: ghosts) {
9+
int pg = Math.abs(target[0] - ghost[0]) + Math.abs(target[1] - ghost[1]);
10+
if(pg <= path) return false;
11+
}
12+
return true;
13+
}
14+
}

0 commit comments

Comments
 (0)