LeetCode 1212: Team Scores in Football Tournament Solution
Master LeetCode problem 1212 (Team Scores in Football Tournament), 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.
1212. Team Scores in Football Tournament
Problem Explanation
Explanation:
To solve this problem, we can use a HashMap to store the team names and their scores. We can iterate through the input list of strings representing the match results and update the scores accordingly. Finally, we can return the top K teams with the highest scores.
Algorithm:
- Initialize an empty HashMap to store team names and their scores.
- Iterate through the input list of strings representing match results.
- For each match result, parse the team names and scores, update the scores in the HashMap accordingly.
- After processing all match results, create a list of teams sorted by their scores in descending order.
- Return the top K teams from the sorted list.
Time Complexity: O(n log n) where n is the number of match results. Space Complexity: O(n) for storing the team scores.
:
Solution Code
import java.util.*;
class Solution {
public List<String> topKFrequent(String[] matches, int k) {
Map<String, Integer> scores = new HashMap<>();
for (String match : matches) {
String[] teams = match.split("\\s+");
String team1 = teams[0];
String team2 = teams[2];
int score1 = Integer.parseInt(teams[1]);
int score2 = Integer.parseInt(teams[3]);
scores.put(team1, scores.getOrDefault(team1, 0) + (score1 > score2 ? 3 : (score1 == score2 ? 1 : 0)));
scores.put(team2, scores.getOrDefault(team2, 0) + (score2 > score1 ? 3 : (score1 == score2 ? 1 : 0)));
}
List<String> result = new ArrayList<>(scores.keySet());
result.sort((team1, team2) -> scores.get(team2) - scores.get(team1));
return result.subList(0, k);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1212 (Team Scores in Football Tournament)?
This page provides optimized solutions for LeetCode problem 1212 (Team Scores in Football Tournament) 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 1212 (Team Scores in Football Tournament)?
The time complexity for LeetCode 1212 (Team Scores in Football Tournament) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1212 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1212 in Java, C++, or Python.