LeetCode 3252: Premier League Table Ranking II Solution
Master LeetCode problem 3252 (Premier League Table Ranking II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3252. Premier League Table Ranking II
Problem Explanation
Explanation:
To solve this problem, we can simulate the matches played and update the team standings accordingly. We will maintain a map to store the team standings based on their points, goals scored, and goal difference. We will iterate through the matches and update the standings for each team based on the match results. Finally, we will sort the teams based on the defined criteria to get the final ranking.
- Create a map to store team standings with team name as the key and a custom class or tuple containing points, goals scored, and goal difference as the value.
- Iterate through each match and update the standings for the teams involved in the match.
- After processing all matches, sort the teams based on the defined criteria (points, goals scored, goal difference) to get the final ranking.
Time Complexity: O(n log n) - Sorting all teams at the end Space Complexity: O(n) - Using a map to store team standings
:
Solution Code
import java.util.*;
class TeamStanding {
int points;
int goalsScored;
int goalDifference;
public TeamStanding(int points, int goalsScored, int goalDifference) {
this.points = points;
this.goalsScored = goalsScored;
this.goalDifference = goalDifference;
}
}
class Solution {
public List<String> premierLeagueTable(int n, List<List<String>> matches) {
Map<String, TeamStanding> standings = new HashMap<>();
for (List<String> match : matches) {
String team1 = match.get(0);
String team2 = match.get(1);
int goals1 = Integer.parseInt(match.get(2));
int goals2 = Integer.parseInt(match.get(3));
standings.putIfAbsent(team1, new TeamStanding(0, 0, 0));
standings.putIfAbsent(team2, new TeamStanding(0, 0, 0));
TeamStanding team1Standing = standings.get(team1);
TeamStanding team2Standing = standings.get(team2);
team1Standing.goalsScored += goals1;
team2Standing.goalsScored += goals2;
team1Standing.goalDifference += (goals1 - goals2);
team2Standing.goalDifference += (goals2 - goals1);
if (goals1 > goals2) {
team1Standing.points += 3;
} else if (goals1 < goals2) {
team2Standing.points += 3;
} else {
team1Standing.points += 1;
team2Standing.points += 1;
}
}
List<String> result = new ArrayList<>(standings.keySet());
Collections.sort(result, (t1, t2) -> {
TeamStanding ts1 = standings.get(t1);
TeamStanding ts2 = standings.get(t2);
if (ts1.points != ts2.points) {
return Integer.compare(ts2.points, ts1.points);
} else if (ts1.goalsScored != ts2.goalsScored) {
return Integer.compare(ts2.goalsScored, ts1.goalsScored);
} else {
return Integer.compare(ts2.goalDifference, ts1.goalDifference);
}
});
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3252 (Premier League Table Ranking II)?
This page provides optimized solutions for LeetCode problem 3252 (Premier League Table Ranking II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3252 (Premier League Table Ranking II)?
The time complexity for LeetCode 3252 (Premier League Table Ranking II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3252 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3252 in Java, C++, or Python.