2225. Find Players With Zero or One Losses
You are given an integer array matches
where matches[i] = [winneri, loseri]
indicates that the player i-th winner
defeated player i-th loser
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 <= 10^5
matches[i].length == 2
1 <= winneri, loseri <= 10^5
winneri != loseri
matches[i]
are unique. 2차원 배열 matches
가 주어진다. 각각 승자(winners
)와 패자(loser
)의 정보가 담겨있다.
한번도 지지 않은 플레이어와 딱 한번만 패배한 플레이어를 리턴해야 한다.
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
answer = [[], []]
winners = collections.Counter()
losers = collections.Counter()
for w, l in matches:
winners[w] += 1
losers[l] += 1
answer[0] = sorted(winners.keys() - losers.keys())
for k, v in sorted(losers.items()):
if v == 1:
answer[1].append(k)
return answer
해설하자면 다음과 같다.
승자의 정보만 담는 winners
와, 패자의 정보만 담는 losers
를 선언한다.
- 키 값은 플레이어의 아이디, 밸류 값은 이긴 횟수 혹은 진 횟수가 된다.
문제에서 주어진 배열 matches
를 순회한다.
- winner일 경우에는 승점을 1점 올려준다.
- loser일 경우에는 패점을 1점 올려준다.
한 번도 패배하지 않은 플레이어를 구한다. (answer[0]
)
- winners의 key 값과 losers의 key값을 차집합을 이용한다면 승자의 정보만 구할 수 있다.
딱 한번 패배한 플레이어를 구한다. (answer[1]
)
- losers
를 순회하며, value값이 1인 경우 즉, 패점이 1점인 경우의 플레이어의 정보(key 값)을 구한다.
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
score = dict() # 패배한 점수만 기록한다.
not_lost_players = []
lose_one_players = []
for w, l in matches:
if w not in score:
score[w] = 0
if l not in score:
score[l] = 1
else:
score[l] += 1
for k, v in score.items():
if v == 0:
not_lost_players.append(k)
elif v == 1:
lose_one_players.append(k)
return [sorted(not_lost_players), sorted(lose_one_players)]
굳이, winners
와 losers
를 선언하지 않고, 하나의 해시만 이용하는 방법이다.
I recently came across the problem [LeetCode] 2225, "Find Players With Zero or One Losses," and I must say, the provided solution is truly impressive! It offers an efficient approach to identify players who have either zero or one loss, which can be quite useful in various scenarios, especially in competitive gaming or sports. why not try these out, more info here, official site, look at this site, check it out.