#2225

Find Players With Zero or One Losses

pupil · 530 · lc medium +28 · verified · 72.5% accepted · 2,267 likes · top 82%

Description

You are given an integer array matches where each matches[i] = [winneri, loseri] records that player winneri defeated player loseri.

Return a list answer of size 2 such that:

- answer[0] contains every player who has never lost a match.

- answer[1] contains every player who has lost exactly one match.

Both sublists must be sorted in ascending order.

Note:

- Only players who appear in at least one match are considered.

- No two matches share 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] = [].

Code

1
2
3