LeetCode 2989: Class Performance Solution
Master LeetCode problem 2989 (Class Performance), 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.
2989. Class Performance
Problem Explanation
Explanation:
To solve this problem, we can use a hashmap to store the performance scores of each student. We will iterate through the given array of scores and calculate the overall performance of the class based on the given rules. Finally, we will return the performance of the class.
Algorithm:
- Create a hashmap to store the performance scores of each student.
- Initialize a variable
classPerformanceto store the overall performance of the class. - Iterate through the given array of scores:
- If the current score is less than 60, increment the performance score by 1.
- If the current score is between 60 and 70 (inclusive), increment the performance score by 2.
- If the current score is between 70 and 80 (inclusive), increment the performance score by 3.
- If the current score is greater than or equal to 80, increment the performance score by 4.
- Return the overall performance of the class.
Time Complexity: O(n) where n is the number of students. Space Complexity: O(n) for the hashmap.
:
Solution Code
class Solution {
public int calculateClassPerformance(int[] scores) {
Map<Integer, Integer> performanceMap = new HashMap<>();
int classPerformance = 0;
for (int score : scores) {
int performance = 0;
if (score < 60) {
performance = 1;
} else if (score >= 60 && score < 70) {
performance = 2;
} else if (score >= 70 && score < 80) {
performance = 3;
} else {
performance = 4;
}
classPerformance += performance;
performanceMap.put(score, performance);
}
return classPerformance;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2989 (Class Performance)?
This page provides optimized solutions for LeetCode problem 2989 (Class Performance) 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 2989 (Class Performance)?
The time complexity for LeetCode 2989 (Class Performance) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2989 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2989 in Java, C++, or Python.