You are given an integer array
matches[i] = [winneri, loseri] indicates that the player
winneri defeated player
loseri in a match.
Return a list
answer of size
answeris a list of all players that have not lost any matches.
answeris a list of all players that have lost exactly one match.
The values in the two lists should be returned in increasing order.
- 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.
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 = [1,2,10] and answer = [4,5,7,8].
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 = [1,2,5,6] and answer = .
1 <= matches.length <= 105
matches[i].length == 2
1 <= winneri, loseri <= 105
winneri != loseri
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.append(player) elif lost[player] == 1: res.append(player) return res