Re: [閒聊] 每日LeetCode已回收
看板Marginalman作者JerryChungYC (JerryChung)時間8月前 (2024/01/16 02:52)推噓0(0推 0噓 0→)留言0則, 0人參與討論串607/719 (看更多)
※ 引述《sustainer123 (caster )》之銘言:
: ※ 引述《yam276 (史萊哲林的優等生)》之銘言:
: : 思路:
: : 1. 先把題目給的陣列分成贏跟輸的HashMap
: : 2. 尋找有在贏Map沒在輸Map的 = Never_loses
: : 3. 尋找輸Map數字是1的 = 一敗仔
: : 4. Sort 因為不是照順序
: Python code:
: class Solution:
: def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
: mc = {}
: pm = {}
: for e in matches:
: if e[0] in mc:
: mc[e[0]] +=1
: else:
: mc[e[0]] = 1
: if e[1] in pm:
: pm[e[1]] +=1
: else:
: pm[e[1]] = 1
: no_losses = sorted([key for key in mc.keys() if key not in pm])
: one_losses = sorted([key for key,value in pm.items() if value == 1])
: return [no_losses,one_losses]
原本用2個list會炸掉 又想不到其他解法只好來偷看
再修改了一些地方
Python3 code:
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
win = {}
lose = {}
for match in matches:
win[match[0]] = 1
lose[match[1]] = 2 if match[1] in lose else 1
winners = sorted([k for k in win if k not in lose])
once = sorted([k for k, v in lose.items() if v == 1])
return [winners, once]
win的value是多少不影響 所以直接給1
lose只需要=1的 所以重複就直接給2
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.28.204 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1705344769.A.A45.html
※ 編輯: JerryChungYC (114.45.8.113 臺灣), 01/16/2024 06:59:56
討論串 (同標題文章)