Skip to content

Commit 68901dc

Browse files
varunu28fishercoder1534
authored andcommitted
Added _789.java (#29)
1 parent 25b2401 commit 68901dc

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Your ideas/fixes/algorithms are more than welcome!
6161
|799|[Champagne Tower](https://leetcode.com/problems/champagne-tower/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_799.java) | O(r^2) or O(1) | O(r^2) or O(1) | |Medium|
6262
|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | O(n) | O(1) | |Easy|
6363
|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | O(n+m) | O(1) | |Medium|
64+
|789|[Escape The Ghosts](https://leetcode.com/problems/escape-the-ghosts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_789.java) | O(n) | O(1) | |Medium| Math|
6465
|788|[Rotated Digits](https://leetcode.com/problems/rotated-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_788.java) | O(n*m) | O(1) | |Easy|
6566
|784|[Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_784.java) | O(n*2^n) | O(n*2^n) | |Easy|
6667
|783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_783.java) | O(n) | O(h) | |Easy|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is
5+
* (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]).
6+
*
7+
* Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or
8+
* south, going from the previous point to a new point 1 unit of distance away.
9+
*
10+
* You escape if and only if you can reach the target before any ghost reaches you (for any given moves the ghosts
11+
* may take.) If you reach any square (including the target) at the same time as a ghost, it doesn't count as an
12+
* escape.
13+
*
14+
* Return True if and only if it is possible to escape.
15+
*/
16+
17+
public class _789 {
18+
19+
public static class Solution {
20+
public boolean escapeGhosts(int[][] ghosts, int[] target) {
21+
int[] currPos = {0, 0};
22+
int selfDist = getDist(currPos, target);
23+
24+
for (int[] ghost : ghosts) {
25+
int ghostDist = getDist(ghost, target);
26+
if (ghostDist <= selfDist) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
private int getDist(int[] p1, int[] p2) {
35+
return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);
36+
}
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._789;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by varunu28 on 1/01/19.
11+
*/
12+
13+
public class _789Test {
14+
private static _789.Solution test;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
test = new _789.Solution();
19+
}
20+
21+
22+
@Test
23+
public void test1() {
24+
assertEquals(true, test.escapeGhosts(new int[][]{{1, 0}, {0, 3}}, new int[]{0, 1}));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
assertEquals(false, test.escapeGhosts(new int[][]{{1, 0}}, new int[]{2, 0}));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
assertEquals(false, test.escapeGhosts(new int[][]{{2, 0}}, new int[]{1, 0}));
35+
}
36+
}

0 commit comments

Comments
 (0)