Sponsored
Sponsored
This approach involves using hash maps (or dictionaries) to track the number of matches each player has won and lost. We loop over the matches to populate these maps. Finally, we iterate over the accumulated data to build the two result lists: players with zero losses and players with exactly one loss. We then sort these lists before returning them.
Time Complexity: O(n log n) due to the sorting of players.
Space Complexity: O(n) for storing player and loss information.
1def findWinners(matches):
2 from collections import defaultdict
3 losses_count = defaultdict(int)
4 players = set()
5 for winner, loser in matches:
6 players.add(winner)
7 players.add(loser)
8 losses_count[loser] += 1
9 zero_losses = [player for player in players if losses_count[player] == 0]
10 one_loss = [player for player in players if losses_count[player] == 1]
11 return [sorted(zero_losses), sorted(one_loss)]
We use a dictionary to count the number of losses for each player. We also maintain a set of all players to ensure that we consider only those who have played matches. By checking the dictionary, we categorize players into those with zero and one loss, sort them, and return the result.
In this approach, we maintain two numerical arrays or lists: one to track whether a player has won, and another to count how many times a player has lost. We then deduce the needed lists by examining these arrays.
Time Complexity: O(n log n)
Space Complexity: O(n)
1function findWinners(matches) {
2 const losses =
This solution uses two arrays: one for checking if a player has played and another for counting losses, similar to the C++ approach. Results are deduced by evaluating these arrays, gathering players with desired loss counts, and sorting them.