精華區beta Marginalman 關於我們 聯絡資訊
624. Maximum Distance in Arrays ## 思路 記錄目前遇到的最大最小值 ## Code ```python class Solution: def maxDistance(self, arrays: List[List[int]]) -> int: n = len(arrays) ans = 0 curr_min, curr_max = arrays[0][0], arrays[0][-1] for i in range(1, n): ans = max(ans, curr_max - arrays[i][0], arrays[i][-1] - curr_min) curr_max = max(curr_max, arrays[i][-1]) curr_min = min(curr_min, arrays[i][0]) return ans ``` -- 每週Premium 3189. Minimum Moves to Get a Peaceful Board ## 思路 Greedy 分開看row跟col移動的步數 每個row跟col都不能重複 sort過後會是[0,1,2,..,n-1] 要移動的步數就是sum(rows[i] - i) + sum(cols[i] - i) ex. rooks = [[0,0],[0,1],[0,2],[0,3]] 建rows = [0, 0, 0, 0] cols = [0, 1, 2, 3] 把三個棋的row移動到1,2,3 -> 1+2+3=6 ## Code ```python class Solution: def minMoves(self, rooks: List[List[int]]) -> int: n = len(rooks) ans = 0 rows = sorted([rooks[i][0] for i in range(n)]) cols = sorted([rooks[i][1] for i in range(n)]) for i in range(n): ans += abs(rows[i] - i) ans += abs(cols[i] - i) return ans ``` -- http://i.imgur.com/OLvBn3b.jpg -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.247 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723771324.A.B4E.html ※ 編輯: dont (185.213.82.247 臺灣), 08/16/2024 09:22:49
DJYOMIYAHINA: 我不小心寫了超多redundany 太苦了 08/16 09:38
DJYOMIYAHINA: premium 好久沒寫了 08/16 09:39
DJYOMIYAHINA: *redundancy 08/16 09:39