Re: [閒聊] 每日LeetCode

作者: JerryChungYC (JerryChung)   2024-01-16 02:52:46
※ 引述《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

Links booklink

Contact Us: admin [ a t ] ucptt.com