1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| class Solution { public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) { Map<String, Integer> wordToScoreMap = new HashMap<>(); for (String positiveWord : positive_feedback) { wordToScoreMap.put(positiveWord, 3); } for (String negativeWord : negative_feedback) { wordToScoreMap.put(negativeWord, -1); }
Queue<int[]> minHeap = new PriorityQueue<>((o1, o2) -> { int diff = o1[1] - o2[1]; if (diff != 0) { return diff; } return o2[0] - o1[0]; });
for (int i = 0; i < report.length; i++) { int[] pair = new int[2]; pair[0] = student_id[i];
String[] words = report[i].split(" "); for (String word : words) { pair[1] += wordToScoreMap.getOrDefault(word, 0); }
minHeap.offer(pair); if (minHeap.size() > k) { minHeap.poll(); } }
List<Integer> idList = new ArrayList<>(k); while (!minHeap.isEmpty()) { idList.add(minHeap.poll()[0]); } Collections.reverse(idList); return idList; } }
|
References
2512. Reward Top K Students