Top Two Teams by Points, Then Goal Difference, Then Goals Scored
Capital OneSoftware Engineer · HR ScreenLast updated
Given parallel arrays of wins, draws, goals scored and goals conceded for each team, compute points as three per win plus one per draw and return the indices of the top two teams. Ties are broken by goal difference, then goals scored, then index, testing multi-key comparison and careful tie handling.
A league records four statistics for each of its n teams in four parallel arrays: wins[i], draws[i], scored[i] (goals scored) and conceded[i] (goals conceded) all describe team i. Rank the teams and return the indices of the first-placed and second-placed teams.
A team earns 3 points for each win and 1 point for each draw, so team i has points = 3 * wins[i] + draws[i].
Function Signature
def top_two_teams(wins: list[int], draws: list[int], scored: list[int], conceded: list[int]) -> list[int]:
Rules
Teams are ranked by the following keys, each one used only to break a tie in all of the previous keys:
- More points ranks higher.
- Greater goal difference,
scored[i] - conceded[i], ranks higher. - More goals scored,
scored[i], ranks higher. - The smaller index ranks higher.
Return a list of exactly two indices, [first, second]. Because the last key never ties, the answer is unique.
Constraints
2 <= n <= 100000, wherenis the common length of all four arrays0 <= wins[i] <= 1000and0 <= draws[i] <= 10000 <= scored[i] <= 100000and0 <= conceded[i] <= 100000- Goal difference can be negative.
Examples
Example 1
- Input:
wins = [1, 3, 2, 3],draws = [2, 0, 3, 0],scored = [5, 7, 6, 9],conceded = [5, 3, 5, 5] - Output:
[3, 1] - Explanation: Points are
[5, 9, 9, 9]. Teams1,2and3tie on 9 points. Their goal differences are4,1and4, so team2drops behind. Teams1and3still tie, and team3scored more goals (9 against 7), so team3is first and team1is second.
Example 2
- Input:
wins = [2, 2],draws = [1, 1],scored = [4, 4],conceded = [2, 2] - Output:
[0, 1] - Explanation: The two teams tie on every statistic, so the smaller index ranks higher.
Example 3
- Input:
wins = [0, 4, 3],draws = [5, 0, 4],scored = [2, 10, 3],conceded = [1, 2, 3] - Output:
[2, 1] - Explanation: Points are
[5, 12, 13]. Team2is first on points even though team1has the far better goal difference (8 against 0); goal difference only matters when points are equal.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.