Find Players With Zero or One Losses

You are given matches where matches[i] = [winner_i, loser_i] means winner_i beat loser_i. Return a list answer of size 2: answer[0] holds every player who has not lost any match, and answer[1] holds every player who lost exactly one match, both in increasing order. Only players who played at least one match are considered.

30:00

Find Players With Zero or One Losses
medium
Free written solution
Topics
Companies

You are given matches where matches[i] = [winner_i, loser_i] means winner_i beat loser_i. Return a list answer of size 2: answer[0] holds every player who has not lost any match, and answer[1] holds every player who lost exactly one match, both in increasing order. Only players who played at least one match are considered.

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]]
Constraints:
  • 1≤matches.length≤1051 \leq \text{matches.length} \leq 10^5

  • matches[i].length==2\text{matches}[i].\text{length} == 2

  • 1≤winneri,loseri≤1051 \leq \text{winner}_i, \text{loser}_i \leq 10^5

  • winneri≠loseri\text{winner}_i \neq \text{loser}_i

  • All matches[i]\text{matches}[i] are unique.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"matches":[[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]}

10 match results, each written as [winner, loser].

match results

1beat3
2beat3
3beat6
5beat6
5beat7
4beat5
4beat8
4beat9
10beat4
10beat9

loss ledger (players in increasing order)

no players yet

answer[0]: never lost

empty

answer[1]: lost once

empty

Variables
VariableValue
matches10
DepthFunction Call
Stack empty
0/33