엔지니어 게시판
LeetCode 솔루션 분류

[11/28] 2225. Find Players With Zero or One Losses

컨텐츠 정보

본문

You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.

Return a list answer of size 2 where:

  • answer[0] is a list of all players that have not lost any matches.
  • answer[1] is a list of all players that have lost exactly one match.

The values in the two lists should be returned in increasing order.

Note:

  • You should only consider the players that have played at least one match.
  • The testcases will be generated such that no two matches will have the same outcome.

 

Example 1:

Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
Output: [[1,2,10],[4,5,7,8]]
Explanation:
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].

Example 2:

Input: matches = [[2,3],[1,3],[5,4],[6,4]]
Output: [[1,2,5,6],[]]
Explanation:
Players 1, 2, 5, and 6 have not lost any matches.
Players 3 and 4 each have lost two matches.
Thus, answer[0] = [1,2,5,6] and answer[1] = [].

 

Constraints:

  • 1 <= matches.length <= 105
  • matches[i].length == 2
  • 1 <= winneri, loseri <= 105
  • winneri != loseri
  • All matches[i] are unique.
Accepted
69,948
Submissions
94,539
태그

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 2493 ms, faster than 79.97% of Python3 online submissions for Find Players With Zero or One Losses.
Memory Usage: 70.4 MB, less than 10.26% of Python3 online submissions for Find Players With Zero or One Losses.
from collections import defaultdict
class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        players = set()
        
        res = [[],[]]
        
        lost = defaultdict(int)
        
        for winner, loser in matches:
            players.add(winner); players.add(loser);
            lost[loser]+=1
        
        
        for player in sorted(list(players)):
            if player not in lost:
                res[0].append(player)
            elif lost[player] == 1:
                res[1].append(player)
        
        return res
전체 405 / 5 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 580 명
  • 오늘 방문자 2,452 명
  • 어제 방문자 7,414 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,543 명
알림 0