Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliverwreath/08be4fdfb7db1b3182ee135405f05071 to your computer and use it in GitHub Desktop.
Save oliverwreath/08be4fdfb7db1b3182ee135405f05071 to your computer and use it in GitHub Desktop.
class Solution {
boolean escapeGhosts(int[][] ghosts, int[] target) {
// filter abnormal cases
// if (A == null || A.length == 0) {
// return 0;
// }
if (ghosts == null || ghosts.length == 0) {
return true;
}
int myDistance = getDistance(new int[]{0, 0}, target);
int m = ghosts.length;
for (int i = 0; i < m; i++) {
int tmpDistance = getDistance(ghosts[i], target);
if (tmpDistance <= myDistance) {
return false;
}
}
// return the final result
return true;
}
int getDistance(int[] source, int[] target) {
return Math.abs(source[0] - target[0]) + Math.abs(source[1] - target[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment