Problem Description
Given a list of match results where each match is represented as [winner, loser], return an answer containing two lists: the first list contains players who have never lost a match, and the second list contains players who have lost exactly one match. The answer lists should be sorted in increasing order. Only players that have played at least one match should be considered.
Key Insights
- Use a hash map to count losses for each player.
- Ensure that players who only won matches and never lost are recorded (loss count = 0).
- After processing all matches, iterate through the loss counts to filter players with 0 or 1 loss.
- Sort the resulting lists before returning.
Space and Time Complexity
Time Complexity: O(n + u log u), where n is the number of matches and u is the number of unique players (sorting the players incurs the log factor). Space Complexity: O(u) for storing loss counts for each unique player.
Solution
The solution involves iterating over the matches and using a hash table (dictionary) to track the number of losses per player. For every match, increment the loss count for the loser and ensure the winner is accounted for in the hash table (with 0 losses if not already present). After processing all matches, iterate through the hash table to collect players with 0 losses and those with 1 loss. Finally, sort both lists to satisfy the order requirement. This approach efficiently counts losses and then filters and sorts the players accordingly.