Ctrl K

Unbeaten and Beaten Once

Medium
A chess club records game results: games[i] = [a, b] means player a beat player b. There are no draws, and the same record (same winner and same loser) never appears twice. Return two lists: - the players who have not lost a single game; - the players who have lost exactly one game. Only players who played at least one game count. Each list must be in increasing order.
Example 1
games=[[4, 9], [9, 2], [4, 2], [7, 4]] → [[7], [4, 9]]

Player 7 never lost. Players 4 and 9 lost one game each, while 2 lost twice and is left out.

Example 2
games=[[1, 2], [2, 1], [3, 1]] → [[3], [2]]

Only 3 has no losses; 2 lost once and 1 lost twice.

Example 3
games=[[10, 20]] → [[10], [20]]
Constraints
  • 1 ≤ len(games) ≤ 10⁵
  • len(games[i]) == 2
  • 1 ≤ a, b ≤ 10⁵
  • a ≠ b
  • All records games[i] are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMore Than a Third of the Votes MediumMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumTriples Hitting the Target Medium
loading Python…
Input
games=[[4, 9], [9, 2], [4, 2], [7, 4]]
Expected
[[7], [4, 9]]

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.