LeetCode 2512: Reward Top K Students Solution

Master LeetCode problem 2512 (Reward Top K Students), 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.

2512. Reward Top K Students

Problem Explanation

Explanation:

  • Initialize a hashmap to store the points of each student.
  • Iterate through the reports and update the points of each student based on the feedback words.
  • Sort the students based on points and student IDs in non-increasing order.
  • Return the top k students.

Time Complexity: O(n) where n is the number of feedback reports
Space Complexity: O(n) for storing student points

:

Solution Code

import java.util.*;

class Solution {
    public int[] topKStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
        Map<Integer, Integer> points = new HashMap<>();
        
        for (int i = 0; i < report.length; i++) {
            String[] words = report[i].split(" ");
            int currPoints = 0;
            for (String word : words) {
                if (Arrays.asList(positive_feedback).contains(word)) {
                    currPoints += 3;
                } else if (Arrays.asList(negative_feedback).contains(word)) {
                    currPoints -= 1;
                }
            }
            points.put(student_id[i], points.getOrDefault(student_id[i], 0) + currPoints);
        }
        
        List<Map.Entry<Integer, Integer>> sortedPoints = new ArrayList<>(points.entrySet());
        Collections.sort(sortedPoints, (a, b) -> a.getValue() != b.getValue() ? b.getValue() - a.getValue() : a.getKey() - b.getKey());
        
        int[] result = new int[k];
        for (int i = 0; i < k; i++) {
            result[i] = sortedPoints.get(i).getKey();
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2512 (Reward Top K Students)?

This page provides optimized solutions for LeetCode problem 2512 (Reward Top K Students) 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 2512 (Reward Top K Students)?

The time complexity for LeetCode 2512 (Reward Top K Students) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2512 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2512 in Java, C++, or Python.

Back to LeetCode Solutions