本文最后更新于 361 天前,其中的信息可能已经有所发展或是发生改变。
题目描述:
给你两个字符串数组 positive_feedback
和 negative_feedback
,分别包含表示正面的和负面的词汇。不会 有单词同时是正面的和负面的。
一开始,每位学生分数为 0
。每个正面的单词会给学生的分数 加 3
分,每个负面的词会给学生的分数 减 1
分。
给你 n
个学生的评语,用一个下标从 0 开始的字符串数组 report
和一个下标从 0 开始的整数数组 student_id
表示,其中 student_id[i]
表示这名学生的 ID ,这名学生的评语是 report[i]
。每名学生的 ID 互不相同。
给你一个整数 k
,请你返回按照得分 从高到低 最顶尖的 k
名学生。如果有多名学生分数相同,ID 越小排名越前。
示例 1:
输入:positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious","the student is smart"], student_id = [1,2], k = 2
输出:[1,2]
解释:
两名学生都有 1 个正面词汇,都得到 3 分,学生 1 的 ID 更小所以排名更前。
示例 2:
输入:positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious","the student is smart"], student_id = [1,2], k = 2
输出:[2,1]
解释:
- ID 为 1 的学生有 1 个正面词汇和 1 个负面词汇,所以得分为 3-1=2 分。
- ID 为 2 的学生有 1 个正面词汇,得分为 3 分。
学生 2 分数更高,所以返回 [2,1] 。
提示:
1 <= positive_feedback.length, negative_feedback.length <= 10^4
1 <= positive_feedback[i].length, negative_feedback[j].length <= 100
positive_feedback[i]
和negative_feedback[j]
都只包含小写英文字母。positive_feedback
和negative_feedback
中不会有相同单词。n == report.length == student_id.length
1 <= n <= 10^4
report[i]
只包含小写英文字母和空格' '
。report[i]
中连续单词之间有单个空格隔开。1 <= report[i].length <= 100
1 <= student_id[i] <= 10^9
student_id[i]
的值 互不相同 。1 <= k <= n
解法一:模拟+哈希表
算法思路:
首先把影响的单纯存入哈希表,其中正面词汇为 3
分,负面词汇为 -1
分。
对于每个学生,把他的评语按空格分割成单词,再根据哈希表中的分值计算总分 score
,再把 [studeng_id, score]
存入数组。
对这个数组进行排序,优先对 score
排序,如相等,再对 student_id
排序。
返回前 k
个学生的 id
。
代码实现:
class Solution {
public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
int n = student_id.length;
Map<String, Integer> map = new HashMap<>();
for(String str : positive_feedback){
map.put(str, 3);
}
for(String str : negative_feedback){
map.put(str, -1);
}
int[][] arr = new int[n][2];
for(int i = 0; i < n; i++){
int score = 0;
String[] words = report[i].split(" ");
for(String word : words){
score += map.getOrDefault(word, 0);
}
arr[i] = new int[] {student_id[i], score};
}
Arrays.sort(arr, (a, b) -> a[1] == b[1] ? a[0] - b[0] : b[1] - a[1]);
List<Integer> ans = new ArrayList<>();
for(int i = 0; i < k; i++){
ans.add(arr[i][0]);
}
return ans;
}
}
复杂度分析:
时间复杂度: O(nlogn),其中 n 为数组 reports 的长度。
空间复杂度: O(n),其中 n 为数组 reports 的长度。