Skip to content

Commit 0872b7d

Browse files
committed
feat: add solutions to lc problem: No.0789
No.0789.Escape The Ghosts
1 parent dc3e088 commit 0872b7d

File tree

7 files changed

+207
-14
lines changed

7 files changed

+207
-14
lines changed

solution/0700-0799/0789.Escape The Ghosts/README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,99 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:曼哈顿距离**
60+
61+
对于任意一个阻碍者,如果它到目的地的曼哈顿距离小于等于你到目的地的曼哈顿距离,那么它就可以在你到达目的地之前抓住你。因此,我们只需要判断所有阻碍者到目的地的曼哈顿距离是否都大于你到目的地的曼哈顿距离即可。
62+
63+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为阻碍者的数量。
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**
6268

6369
<!-- 这里可写当前语言的特殊实现逻辑 -->
6470

6571
```python
66-
72+
class Solution:
73+
def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:
74+
tx, ty = target
75+
return all(abs(tx - x) + abs(ty - y) > abs(tx) + abs(ty) for x, y in ghosts)
6776
```
6877

6978
### **Java**
7079

7180
<!-- 这里可写当前语言的特殊实现逻辑 -->
7281

7382
```java
83+
class Solution {
84+
public boolean escapeGhosts(int[][] ghosts, int[] target) {
85+
int tx = target[0], ty = target[1];
86+
for (var g : ghosts) {
87+
int x = g[0], y = g[1];
88+
if (Math.abs(tx - x) + Math.abs(ty - y) <= Math.abs(tx) + Math.abs(ty)) {
89+
return false;
90+
}
91+
}
92+
return true;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
103+
int tx = target[0], ty = target[1];
104+
for (auto& g : ghosts) {
105+
int x = g[0], y = g[1];
106+
if (abs(tx - x) + abs(ty - y) <= abs(tx) + abs(ty)) {
107+
return false;
108+
}
109+
}
110+
return true;
111+
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func escapeGhosts(ghosts [][]int, target []int) bool {
119+
tx, ty := target[0], target[1]
120+
for _, g := range ghosts {
121+
x, y := g[0], g[1]
122+
if abs(tx-x)+abs(ty-y) <= abs(tx)+abs(ty) {
123+
return false
124+
}
125+
}
126+
return true
127+
}
128+
129+
func abs(x int) int {
130+
if x < 0 {
131+
return -x
132+
}
133+
return x
134+
}
135+
```
74136

137+
### **TypeScript**
138+
139+
```ts
140+
function escapeGhosts(ghosts: number[][], target: number[]): boolean {
141+
const [tx, ty] = target;
142+
for (const [x, y] of ghosts) {
143+
if (
144+
Math.abs(tx - x) + Math.abs(ty - y) <=
145+
Math.abs(tx) + Math.abs(ty)
146+
) {
147+
return false;
148+
}
149+
}
150+
return true;
151+
}
75152
```
76153

77154
### **...**

solution/0700-0799/0789.Escape The Ghosts/README_EN.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,84 @@
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:
61+
tx, ty = target
62+
return all(abs(tx - x) + abs(ty - y) > abs(tx) + abs(ty) for x, y in ghosts)
6063
```
6164

6265
### **Java**
6366

6467
```java
68+
class Solution {
69+
public boolean escapeGhosts(int[][] ghosts, int[] target) {
70+
int tx = target[0], ty = target[1];
71+
for (var g : ghosts) {
72+
int x = g[0], y = g[1];
73+
if (Math.abs(tx - x) + Math.abs(ty - y) <= Math.abs(tx) + Math.abs(ty)) {
74+
return false;
75+
}
76+
}
77+
return true;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
88+
int tx = target[0], ty = target[1];
89+
for (auto& g : ghosts) {
90+
int x = g[0], y = g[1];
91+
if (abs(tx - x) + abs(ty - y) <= abs(tx) + abs(ty)) {
92+
return false;
93+
}
94+
}
95+
return true;
96+
}
97+
};
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
func escapeGhosts(ghosts [][]int, target []int) bool {
104+
tx, ty := target[0], target[1]
105+
for _, g := range ghosts {
106+
x, y := g[0], g[1]
107+
if abs(tx-x)+abs(ty-y) <= abs(tx)+abs(ty) {
108+
return false
109+
}
110+
}
111+
return true
112+
}
113+
114+
func abs(x int) int {
115+
if x < 0 {
116+
return -x
117+
}
118+
return x
119+
}
120+
```
65121

122+
### **TypeScript**
123+
124+
```ts
125+
function escapeGhosts(ghosts: number[][], target: number[]): boolean {
126+
const [tx, ty] = target;
127+
for (const [x, y] of ghosts) {
128+
if (
129+
Math.abs(tx - x) + Math.abs(ty - y) <=
130+
Math.abs(tx) + Math.abs(ty)
131+
) {
132+
return false;
133+
}
134+
}
135+
return true;
136+
}
66137
```
67138

68139
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
4+
int tx = target[0], ty = target[1];
5+
for (auto& g : ghosts) {
6+
int x = g[0], y = g[1];
7+
if (abs(tx - x) + abs(ty - y) <= abs(tx) + abs(ty)) {
8+
return false;
9+
}
10+
}
11+
return true;
12+
}
13+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func escapeGhosts(ghosts [][]int, target []int) bool {
2+
tx, ty := target[0], target[1]
3+
for _, g := range ghosts {
4+
x, y := g[0], g[1]
5+
if abs(tx-x)+abs(ty-y) <= abs(tx)+abs(ty) {
6+
return false
7+
}
8+
}
9+
return true
10+
}
11+
12+
func abs(x int) int {
13+
if x < 0 {
14+
return -x
15+
}
16+
return x
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean escapeGhosts(int[][] ghosts, int[] target) {
3+
int tx = target[0], ty = target[1];
4+
for (var g : ghosts) {
5+
int x = g[0], y = g[1];
6+
if (Math.abs(tx - x) + Math.abs(ty - y) <= Math.abs(tx) + Math.abs(ty)) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
}
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
class Solution:
2-
def escapeGhosts(self, ghosts, target):
3-
"""
4-
:type ghosts: List[List[int]]
5-
:type target: List[int]
6-
:rtype: bool
7-
"""
8-
flag = abs(target[0]) + abs(target[1])
9-
for i in ghosts:
10-
if abs(i[0] - target[0]) + abs(i[1] - target[1]) <= flag:
11-
return False
12-
else:
13-
return True
2+
def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:
3+
tx, ty = target
4+
return all(abs(tx - x) + abs(ty - y) > abs(tx) + abs(ty) for x, y in ghosts)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function escapeGhosts(ghosts: number[][], target: number[]): boolean {
2+
const [tx, ty] = target;
3+
for (const [x, y] of ghosts) {
4+
if (
5+
Math.abs(tx - x) + Math.abs(ty - y) <=
6+
Math.abs(tx) + Math.abs(ty)
7+
) {
8+
return false;
9+
}
10+
}
11+
return true;
12+
}

0 commit comments

Comments
 (0)