LeetCode 1841: League Statistics Solution
Master LeetCode problem 1841 (League Statistics), 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.
1841. League Statistics
Problem Explanation
Explanation:
We are given a list of strings representing the results of matches in a league. Each string contains the names of two teams and the score in the format "team1#score1@score2#team2". Our task is to calculate the total points of each team in the league.
To solve this problem, we will iterate through each match result, extract the team names and scores, update the points for each team based on the match outcome, and finally return a map or dictionary containing the total points for each team.
Algorithm:
- Create a hashmap to store the points for each team.
- Iterate through each match result string in the input list.
- Split the match result string to extract team names and scores.
- Compare the scores to determine the winning team and update their points accordingly.
- Return the hashmap containing the total points for each team.
Time Complexity: O(n) where n is the number of match results in the input list. Space Complexity: O(t) where t is the number of unique team names.
:
Solution Code
import java.util.*;
class Solution {
public Map<String, Integer> calculatePoints(List<String> matches) {
Map<String, Integer> points = new HashMap<>();
for (String match : matches) {
String[] parts = match.split("#|@");
String team1 = parts[0];
String team2 = parts[2];
int score1 = Integer.parseInt(parts[1]);
int score2 = Integer.parseInt(parts[3]);
if (score1 > score2) {
points.put(team1, points.getOrDefault(team1, 0) + 3);
} else if (score2 > score1) {
points.put(team2, points.getOrDefault(team2, 0) + 3);
} else {
points.put(team1, points.getOrDefault(team1, 0) + 1);
points.put(team2, points.getOrDefault(team2, 0) + 1);
}
}
return points;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1841 (League Statistics)?
This page provides optimized solutions for LeetCode problem 1841 (League Statistics) 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 1841 (League Statistics)?
The time complexity for LeetCode 1841 (League Statistics) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1841 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1841 in Java, C++, or Python.