32 lines
1.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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]));