Find Players With Zero or One Losses - Problem

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.

Input & Output

Example 1 — Basic Tournament
$ 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,6]]
💡 Note: Players 1, 2, 10 never lost (0 losses). Players 4, 5, 6 lost exactly once. Player 3 lost twice (excluded), players 7, 8, 9 lost once but 7, 8, 9 are not winners so they're excluded from first list.
Example 2 — Simple Case
$ Input: matches = [[2,3],[1,3],[3,4]]
Output: [[1,2],[3]]
💡 Note: Players 1 and 2 never lost. Player 3 lost exactly once. Player 4 lost once but since 4 never won, only players who played are considered.
Example 3 — Single Match
$ Input: matches = [[1,2]]
Output: [[1],[2]]
💡 Note: Player 1 won and never lost (0 losses). Player 2 lost exactly once.

Constraints

  • 1 ≤ matches.length ≤ 105
  • matches[i].length == 2
  • 1 ≤ winneri, loseri ≤ 105
  • winneri ≠ loseri
  • All matches[i] are unique

Visualization

Tap to expand
Find Players With Zero or One LossesInput: Matches[1,3][2,3][3,6][4,5]Loss CountPlayer 1: 0 losses ✓Player 2: 0 losses ✓Player 3: 2 losses ✗Player 5: 1 loss ✓Results (Sorted)0 Losses[1, 2, 4]1 Loss[5, 6]Output: [[1,2,4], [5,6]]
Understanding the Visualization
1
Input Matches
Array of [winner, loser] pairs from tournament
2
Count Losses
Track how many times each player lost
3
Categorize & Sort
Group players by 0 or 1 losses, return sorted lists
Key Takeaway
🎯 Key Insight: Use hash map to count losses efficiently in one pass, then filter and sort by loss count
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
83.2K Views
Medium Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen