aboutsummaryrefslogtreecommitdiff
path: root/find_players_zero_loss.py
blob: 428e3068b9d6332ccd4b3c2dae431d65512b22d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 2225. Find Players With Zero or One Losses

"""
given an integer array 'matches' where 'matches[i] = [winner[i], loser[i]]'
indicates a defeated player 'loser[i]' 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.
"""


class Solution(object):
    def findWinners(self, matches):
        """
        :type matches: List[List[int]]
        :rtype: List[List[int]]
        """
        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]


if __name__ == "__main__":
    obj = Solution()
    print(
        obj.findWinners(
            matches=[
                [1, 3],
                [2, 3],
                [3, 6],
                [5, 6],
                [5, 7],
                [4, 5],
                [4, 8],
                [4, 9],
                [10, 4],
                [10, 9],
            ]
        )
    )
    print(obj.findWinners(matches=[[2, 3], [1, 3], [5, 4], [6, 4]]))