LC.P2512[奖励最顶尖的K名学生]

方法一:哈希表+排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
Set<String> positiveSet = new HashSet<>(List.of(positive_feedback));
Set<String> negativeSet = new HashSet<>(List.of(negative_feedback));
int n = report.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
int sid = student_id[i], t = 0;
for (String s : report[i].split(" ")) {
if (positiveSet.contains(s)) t += 3;
if (negativeSet.contains(s)) t -= 1;
}
arr[i] = new int[]{t, sid};
}
Arrays.sort(arr, (a, b) -> a[0] != b[0] ? b[0] - a[0] : a[1] - b[1]);
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < k; ++i) {
ans.add(arr[i][1]);
}
return ans;
}
}