LeetCode 3246: Premier League Table Ranking Solution
Master LeetCode problem 3246 (Premier League Table Ranking), a easy 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.
3246. Premier League Table Ranking
Problem Explanation
Explanation
To solve this problem, we need to implement a Premier League table ranking system. We can create a class representing a team with its name, number of wins, number of draws, and number of losses. We can then sort the teams based on their points, where a win gives 3 points, a draw gives 1 point, and a loss gives 0 points. In case of a tie in points, we can use goal difference as a tiebreaker.
- Create a Team class with name, wins, draws, losses, goalsFor, and goalsAgainst attributes.
- Parse the input data to create Team objects and update their statistics.
- Sort the teams based on points and goal difference.
- Generate the final ranking based on the sorted teams.
Time complexity: O(nlogn) - sorting the teams Space complexity: O(n) - storing team objects
Solution Code
import java.util.*;
class Team {
String name;
int wins;
int draws;
int losses;
int goalsFor;
int goalsAgainst;
public Team(String name) {
this.name = name;
this.wins = 0;
this.draws = 0;
this.losses = 0;
this.goalsFor = 0;
this.goalsAgainst = 0;
}
public int getPoints() {
return wins * 3 + draws;
}
public int getGoalDifference() {
return goalsFor - goalsAgainst;
}
}
class Solution {
public List<String> premierLeagueTable(String[][] results) {
Map<String, Team> teams = new HashMap<>();
for (String[] result : results) {
String team1 = result[0];
String team2 = result[1];
int goals1 = Integer.parseInt(result[2]);
int goals2 = Integer.parseInt(result[3]);
teams.putIfAbsent(team1, new Team(team1));
teams.putIfAbsent(team2, new Team(team2));
Team t1 = teams.get(team1);
Team t2 = teams.get(team2);
t1.goalsFor += goals1;
t1.goalsAgainst += goals2;
t2.goalsFor += goals2;
t2.goalsAgainst += goals1;
if (goals1 > goals2) {
t1.wins++;
t2.losses++;
} else if (goals1 < goals2) {
t1.losses++;
t2.wins++;
} else {
t1.draws++;
t2.draws++;
}
}
List<Team> teamList = new ArrayList<>(teams.values());
Collections.sort(teamList, (a, b) -> {
if (a.getPoints() != b.getPoints()) {
return b.getPoints() - a.getPoints();
} else {
return b.getGoalDifference() - a.getGoalDifference();
}
});
List<String> result = new ArrayList<>();
for (int i = 0; i < teamList.size(); i++) {
result.add((i + 1) + ". " + teamList.get(i).name);
}
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3246 (Premier League Table Ranking)?
This page provides optimized solutions for LeetCode problem 3246 (Premier League Table Ranking) 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 3246 (Premier League Table Ranking)?
The time complexity for LeetCode 3246 (Premier League Table Ranking) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3246 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3246 in Java, C++, or Python.