LeetCode 1112: Highest Grade For Each Student Solution
Master LeetCode problem 1112 (Highest Grade For Each Student), 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.
1112. Highest Grade For Each Student
Problem Explanation
Explanation:
To solve this problem, we need to find the highest grade for each student in a table where the rows represent students and the columns represent exams. We can iterate through each column and keep track of the highest grade for each student.
- Create a map where the key is the student id and the value is the highest grade.
- Iterate through each row of the table.
- For each student id, update the highest grade if the current grade is higher.
- Finally, return the map with the highest grade for each student.
Time Complexity: O(n * m) where n is the number of students and m is the number of exams.
Space Complexity: O(n) where n is the number of students.
:
Solution Code
class Solution {
public Map<Integer, Integer> highestGradeForEachStudent(int[][] table) {
Map<Integer, Integer> highestGrades = new HashMap<>();
for (int[] row : table) {
int studentId = row[0];
int grade = row[1];
highestGrades.put(studentId, highestGrades.getOrDefault(studentId, 0) < grade ? grade : highestGrades.getOrDefault(studentId, 0));
}
return highestGrades;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1112 (Highest Grade For Each Student)?
This page provides optimized solutions for LeetCode problem 1112 (Highest Grade For Each Student) 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 1112 (Highest Grade For Each Student)?
The time complexity for LeetCode 1112 (Highest Grade For Each Student) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1112 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1112 in Java, C++, or Python.