Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/09/04 10:58), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串804/1548 (看更多)
874. Walking Robot Simulation ## 思路 先把obstacles改為set 方便檢查 然後模擬 記錄command移動後的最大距離平方和 ## Code ```python class Solution: def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int: obstacles = {(x, y) for x, y in obstacles} res = 0 x = y = i = 0 dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] for cmd in commands: if cmd == -1: i = (i+1) % 4 elif cmd == -2: i = (i-1) % 4 else: for _ in range(cmd): nx, ny = x + dirs[i][0], y + dirs[i][1] if (nx, ny) in obstacles: break x, y = nx, ny res = max(res, x * x + y * y) return res ``` -- https://i.imgur.com/kyBhy6o.jpeg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.51 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725418739.A.DFC.html
文章代碼(AID): #1cryppty (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cryppty (Marginalman)