forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0874.py
More file actions
20 lines (20 loc) · 656 Bytes
/
0874.py
File metadata and controls
20 lines (20 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
d = [(0, 1), (1, 0), (0, -1), (-1, 0)]
tmp = res = 0
x, y = 0, 0
ob = set(map(tuple, obstacles))
for i in commands:
if i == -1:
tmp += 1
elif i == -2:
tmp -= 1
else:
for _ in range(i):
x += d[tmp%4][0]
y += d[tmp%4][1]
if (x, y) in ob:
x -= d[tmp%4][0]
y -= d[tmp%4][1]
res = max(res, x**2 + y**2)
return res