Skip to content

Added _789.java #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Your ideas/fixes/algorithms are more than welcome!
|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|
|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | O(n) | O(1) | |Easy|
|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|
|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|
|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|
|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|
|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|
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/fishercoder/solutions/_789.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.fishercoder.solutions;

/**
* You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is
* (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]).
*
* Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or
* south, going from the previous point to a new point 1 unit of distance away.
*
* You escape if and only if you can reach the target before any ghost reaches you (for any given moves the ghosts
* may take.) If you reach any square (including the target) at the same time as a ghost, it doesn't count as an
* escape.
*
* Return True if and only if it is possible to escape.
*/

public class _789 {

public static class Solution {
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int[] currPos = {0, 0};
int selfDist = getDist(currPos, target);

for (int[] ghost : ghosts) {
int ghostDist = getDist(ghost, target);
if (ghostDist <= selfDist) {
return false;
}
}

return true;
}

private int getDist(int[] p1, int[] p2) {
return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);
}
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/fishercoder/_789Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.fishercoder;

import com.fishercoder.solutions._789;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by varunu28 on 1/01/19.
*/

public class _789Test {
private static _789.Solution test;

@BeforeClass
public static void setup() {
test = new _789.Solution();
}


@Test
public void test1() {
assertEquals(true, test.escapeGhosts(new int[][]{{1, 0}, {0, 3}}, new int[]{0, 1}));
}

@Test
public void test2() {
assertEquals(false, test.escapeGhosts(new int[][]{{1, 0}}, new int[]{2, 0}));
}

@Test
public void test3() {
assertEquals(false, test.escapeGhosts(new int[][]{{2, 0}}, new int[]{1, 0}));
}
}