class Solution:
def findWinners(self, matches):
losses = [0] * 100001
for winner, loser in matches:
if losses[winner] == 0:
losses[winner] = -1
if losses[loser] == -1:
losses[loser] = 1
else:
losses[loser] += 1
zero_loss = [i for i in range(1, 100001) if losses[i] == -1]
one_loss = [i for i in range(1, 100001) if losses[i] == 1]
return [zero_loss, one_loss]
'''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.
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] = [].'''| Associated Context | |
|---|---|
| Type | Code Snippet ( .py ) |
| Associated Tags | Solution class findWinners method matches parameter losses list winner variable loser variable zero_loss variable one_loss variable range function return statement numpy zero_loss list one_loss list indexing if-else statements |
| π Custom Description | YouTube Video Explanation: Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. |
| π‘ Smart Description | This code snippet finds the number of winners in a given list that match any matches. It uses an array to keep track of all possible losses, and then returns them as lists with zero or one loss values for each winner/loser pair The code snippet takes a list of matches as input and determines the winners and losers. It keeps track of the number of losses for each player and returns two lists: one with players who have zero losses and another with players who have one loss. |
| π Suggested Searches | Python code to find winners in a list |
| Related Links | https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/4567095/Beats-100-C%2B%2BJavaPythonJS-Explained-with-Video-Hash-Count https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/ https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/?currentPage=1&orderBy=hot&query= https://leetcode.com/problemset/ https://www.geeksforgeeks.org/python-string/ https://www.geeksforgeeks.org/python-lists/ https://www.geeksforgeeks.org/python-functions/ https://www.pythoncentral.io/how-to-sort-a-list-tuple-or-object-with-sorted-in-python/ https://realpython.com/iterate-through-dictionary-python/ https://www.techiedelight.com/find-first-non-repeating-character-string-one-traversal/ https://www.codingame.com/playgrounds/1053/find-the-winners-of-all-competitions https://www.codewars.com/kata/5e4eb72bb95d28002dbbecde |
| Related People | Rohan Mallick |
| Sensitive Information | No Sensitive Information Detected |
| Shareable Link | https://rohan016.pieces.cloud/?p=6e0a4a81c1 |