看板 Marginalman
https://leetcode.com/problems/walking-robot-simulation ※ 引述《enmeitiryous (enmeitiryous)》之銘言: : 題目: 874 walking robot simulation : 一個機器人起始在(0,0)並面向+y的方向,給你一串指令 commands,當coomands[i]=-1代 : 表向右轉,-2代表向左轉,否則代表朝面向方向走的距離,給你一個blockers座標vector : 如果機器人的下一步存在該列表中則機器人原地停下,求在過程中最大的x**2+y**2 : 思路: : 照做,先把blockers塞到一個set,每一次移動時看下一步是不是在blockers中,並記錄 : 每一次移動完x**2+y**2需不需要更新 思路:差不多 Python Code: class Solution: def robotSim(self,commands: List[int],obstacles: List[List[int]]) -> int: arr = ((0, 1), (-1, 0), (0, -1), (1, 0)) idx = 0 now = (0, 0) ans = 0 obstacles_set = set(tuple(obs) for obs in obstacles) for command in commands: if command == -1: idx -= 1 idx %= 4 elif command == -2: idx += 1 idx %= 4 else: while command: x, y = now[0] + arr[idx][0], now[1] + arr[idx][1] if (x, y) in obstacles_set: break now = (x, y) command -= 1 ans = max(ans, now[0] ** 2 + now[1] ** 2) return ans 先轉為 set 再搜尋 原本用 list 直接 6000ms 接 TLE set 就快很多了 距離則等當次走完再算就好 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.52.67 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725419454.A.C0A.html