-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
26 lines (24 loc) · 766 Bytes
/
solution.py
File metadata and controls
26 lines (24 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution(object):
def robotSim(self, commands, obstacles):
"""
:type commands: List[int]
:type obstacles: List[List[int]]
:rtype: int
"""
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
x = y = di = 0
obstacleSet = set(map(tuple, obstacles))
ans = 0
for cmd in commands:
if cmd == -2: #left
di = (di - 1) % 4
elif cmd == -1: #right
di = (di + 1) % 4
else:
for k in xrange(cmd):
if (x + dx[di], y + dy[di]) not in obstacleSet:
x += dx[di]
y += dy[di]
ans = max(ans, x * x + y * y)
return ans