32 lines
1.0 KiB
JavaScript
32 lines
1.0 KiB
JavaScript
/**
|
||
* https://leetcode.cn/problems/h-index/?envType=study-plan-v2&envId=top-interview-150
|
||
* @param {number[]} citations
|
||
* @return {number}
|
||
*/
|
||
const hIndex = function (citations) {
|
||
|
||
};
|
||
|
||
/*
|
||
思路:利用贪心算法,遍历整个数组,假设默认是h0,表示有至少零篇论文被引用了至少零次,[0][0,0] 这些都满足,我们遍历的时候检查有没有
|
||
符合h+1的论文,如果有的化就h++,说明h指数应该增加了,否则的话就表示这个人的学术水平就是当前h,没有必要找下面的h了,直接break
|
||
*/
|
||
function f1(citations) {
|
||
const n = citations.length; // 论文的数量
|
||
citations.sort((a, b) => b - a); // 按引用次数降序排序
|
||
let h = 0; // h指数
|
||
|
||
// 遍历排序后的数组,寻找最大符合条件的 h 指数
|
||
for (let i = 0; i < n; i++) {
|
||
if (citations[i] >= h + 1) { // 如果当前论文引用次数大于等于 h+1
|
||
h++; // 更新 h 指数
|
||
} else {
|
||
break; // 如果不满足条件,则退出循环
|
||
}
|
||
}
|
||
|
||
return h;
|
||
}
|
||
|
||
console.log(f1([1, 3, 1]));
|