Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 11. 28. 23:45

https://leetcode.com/problems/find-players-with-zero-or-one-losses/

 

Find Players With Zero or One Losses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

map을 이용해서 풀 수 있는 간단한 문제!

class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        res = {}
        for match in matches:
            w, l = match[0], match[1]
            if not res.get(l):
                res[l] = 1                
                if not res.get(w):
                    res[w] = 0                
                continue
            if not res.get(w):
                res[w] = 0                                    
            res[l] += 1
        
        sorted_res = sorted(res.items())
        
        zero, one = [], []    
        for r in sorted_res:
            if r[1] == 0:
                zero.append(r[0])
            if r[1] == 1:
                one.append(r[0])
        
        return [zero, one]
반응형