2512. Reward Top K Students LeetCode Solution

In this guide, you will get 2512. Reward Top K Students LeetCode Solution with the best time and space complexity. The solution to Reward Top K Students problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Reward Top K Students solution in C++
  4. Reward Top K Students solution in Java
  5. Reward Top K Students solution in Python
  6. Additional Resources
2512. Reward Top K Students LeetCode Solution image

Problem Statement of Reward Top K Students

You are given two string arrays positive_feedback and negative_feedback, containing the words denoting positive and negative feedback, respectively. Note that no word is both positive and negative.
Initially every student has 0 points. Each positive word in a feedback report increases the points of a student by 3, whereas each negative word decreases the points by 1.
You are given n feedback reports, represented by a 0-indexed string array report and a 0-indexed integer array student_id, where student_id[i] represents the ID of the student who has received the feedback report report[i]. The ID of each student is unique.
Given an integer k, return the top k students after ranking them in non-increasing order by their points. In case more than one student has the same points, the one with the lower ID ranks higher.

Example 1:

Input: positive_feedback = [“smart”,”brilliant”,”studious”], negative_feedback = [“not”], report = [“this student is studious”,”the student is smart”], student_id = [1,2], k = 2
Output: [1,2]
Explanation:
Both the students have 1 positive feedback and 3 points but since student 1 has a lower ID he ranks higher.

Example 2:

Input: positive_feedback = [“smart”,”brilliant”,”studious”], negative_feedback = [“not”], report = [“this student is not studious”,”the student is smart”], student_id = [1,2], k = 2
Output: [2,1]
Explanation:
– The student with ID 1 has 1 positive feedback and 1 negative feedback, so he has 3-1=2 points.
– The student with ID 2 has 1 positive feedback, so he has 3 points.
Since student 2 has more points, [2,1] is returned.

Constraints:

1 <= positive_feedback.length, negative_feedback.length <= 104
1 <= positive_feedback[i].length, negative_feedback[j].length <= 100
Both positive_feedback[i] and negative_feedback[j] consists of lowercase English letters.
No word is present in both positive_feedback and negative_feedback.
n == report.length == student_id.length
1 <= n <= 104
report[i] consists of lowercase English letters and spaces ' '.
There is a single space between consecutive words of report[i].
1 <= report[i].length <= 100
1 <= student_id[i] <= 109
All the values of student_id[i] are unique.
1 <= k <= n

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

2512. Reward Top K Students LeetCode Solution in C++

class Solution {
 public:
  vector<int> topStudents(vector<string>& positive_feedback,
                          vector<string>& negative_feedback,
                          vector<string>& report, vector<int>& student_id,
                          int k) {
    vector<int> ans;
    vector<pair<int, int>> scoreAndIds;
    unordered_set<string> pos{positive_feedback.begin(),
                              positive_feedback.end()};
    unordered_set<string> neg{negative_feedback.begin(),
                              negative_feedback.end()};

    for (int i = 0; i < report.size(); ++i) {
      int score = 0;
      istringstream iss(report[i]);
      for (string word; iss >> word;) {
        if (pos.contains(word))
          score += 3;
        if (neg.contains(word))
          score -= 1;
      }
      scoreAndIds.emplace_back(-score, student_id[i]);
    }

    partial_sort(scoreAndIds.begin(), scoreAndIds.begin() + k,
                 scoreAndIds.end());
    transform(
        scoreAndIds.begin(), scoreAndIds.begin() + k, back_inserter(ans),
        [](const pair<int, int>& scoreAndId) { return scoreAndId.second; });
    return ans;
  }
};
/* code provided by PROGIEZ */

2512. Reward Top K Students LeetCode Solution in Java

class Solution {
  public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback,
                                   String[] report, int[] student_id, int k) {
    List<Integer> ans = new ArrayList<>();
    Pair<Integer, Integer>[] scoreAndIds = new Pair[report.length];
    Set<String> pos = Arrays.stream(positive_feedback).collect(Collectors.toSet());
    Set<String> neg = Arrays.stream(negative_feedback).collect(Collectors.toSet());

    for (int i = 0; i < report.length; ++i) {
      int score = 0;
      for (final String word : report[i].split(" ")) {
        if (pos.contains(word))
          score += 3;
        if (neg.contains(word))
          score -= 1;
      }
      scoreAndIds[i] = new Pair<>(score, student_id[i]);
    }

    Arrays.sort(scoreAndIds,
                Comparator.comparing(Pair<Integer, Integer>::getKey, Comparator.reverseOrder())
                    .thenComparing(Pair<Integer, Integer>::getValue));

    for (int i = 0; i < k; ++i)
      ans.add(scoreAndIds[i].getValue());
    return ans;
  }
}
// code provided by PROGIEZ

2512. Reward Top K Students LeetCode Solution in Python

class Solution:
  def topStudents(
      self,
      positive_feedback: list[str],
      negative_feedback: list[str],
      report: list[str],
      student_id: list[int],
      k: int,
  ) -> list[int]:
    scoreAndIds = []
    pos = set(positive_feedback)
    neg = set(negative_feedback)

    for sid, r in zip(student_id, report):
      score = 0
      for word in r.split():
        if word in pos:
          score += 3
        if word in neg:
          score -= 1
      scoreAndIds.append((-score, sid))

    return [sid for _, sid in sorted(scoreAndIds)[:k]]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.