Skip to content

Commit ccd6a41

Browse files
committed
solve problem Escape The Ghosts
1 parent 8c66ead commit ccd6a41

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ All solutions will be accepted!
304304
|739|[Daily Temperatures](https://leetcode-cn.com/problems/daily-temperatures/description/)|[java/py/js](./algorithms/DailyTemperatures)|Medium|
305305
|921|[Minimum Add To Make Parentheses Valid](https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid/description/)|[java/py/js](./algorithms/MinimumAddToMakeParenthesesValid)|Medium|
306306
|797|[All Paths From Source To Target](https://leetcode-cn.com/problems/all-paths-from-source-to-target/description/)|[java/py/js](./algorithms/AllPathsFromSourceToTarget)|Medium|
307+
|789|[Escape The Ghosts](https://leetcode-cn.com/problems/escape-the-ghosts/description/)|[java/py/js](./algorithms/EscapeTheGhosts)|Medium|
307308

308309
# Database
309310
|#|Title|Solution|Difficulty|

algorithms/EscapeTheGhosts/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Escape The Ghosts
2+
This problem is easy to solve
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean escapeGhosts(int[][] ghosts, int[] target) {
3+
for (int i = 0; i < ghosts.length; i++) {
4+
int[] ghost = ghosts[i];
5+
if (Math.abs(ghost[0] - target[0]) + Math.abs(ghost[1] - target[1]) <= Math.abs(target[0]) + Math.abs(target[1]))
6+
return false;
7+
}
8+
9+
return true;
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[][]} ghosts
3+
* @param {number[]} target
4+
* @return {boolean}
5+
*/
6+
var escapeGhosts = function(ghosts, target) {
7+
for (let i = 0; i < ghosts.length; i++) {
8+
let ghost = ghosts[i]
9+
if (Math.abs(ghost[0] - target[0]) + Math.abs(ghost[1] - target[1]) <= Math.abs(target[0]) + Math.abs(target[1]))
10+
return false
11+
}
12+
13+
return true
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def escapeGhosts(self, ghosts, target):
3+
"""
4+
:type ghosts: List[List[int]]
5+
:type target: List[int]
6+
:rtype: bool
7+
"""
8+
dis = abs(target[0]) + abs(target[1])
9+
10+
for ghost in ghosts:
11+
if abs(ghost[0] - target[0]) + abs(ghost[1] - target[1]) <= dis:
12+
return False
13+
14+
return True

0 commit comments

Comments
 (0)