-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEscapeTheGhosts.java
30 lines (28 loc) · 1.16 KB
/
EscapeTheGhosts.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
/**
* LeetCode
* 789. Escape The Ghosts
* https://leetcode.com/problems/escape-the-ghosts/
* #Medium
*/
public class EscapeTheGhosts {
public static void main(String[] args) {
EscapeTheGhosts sol = new EscapeTheGhosts();
System.out.println(sol.escapeGhosts(new int[][]{{1, 0}, {0, 3}}, new int[]{0, 1})); // true
System.out.println(sol.escapeGhosts(new int[][]{{1, 0}}, new int[]{2, 0})); // false
System.out.println(sol.escapeGhosts(new int[][]{{2, 0}}, new int[]{1, 0})); // false
System.out.println(sol.escapeGhosts(new int[][]{{5, 0}, {-10, -2}, {0, -5}, {-2, -2}, {-7, 1}}, new int[]{7, 7})); // false
System.out.println(sol.escapeGhosts(new int[][]{{-1, 0}, {0, 1}, {-1, 0}, {0, 1}, {-1, 0}}, new int[]{0, 0})); // true
}
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int success = dist(target, new int[]{0, 0});
for (int[] ghost : ghosts) {
if (success >= dist(target, ghost)) {
return false;
}
}
return true;
}
private int dist(int[] a, int[] b) {
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
}
}