题目描述
分析
题目有点绕,实际上题目要我们找到一个最大的 h
算法
排序,贪心
过程
生序排序数组
从最后一位开始,令 h = 1,开始计算 h 指数
找到一个非法的值或遍历完整个数组后,返回 h
代码
/**
* @param {number[]} citations
* @return {number}
*/
var hIndex = function (citations) {
citations.sort((a, b) => a - b)
const n = citations.length
let h = 1
while (h <= n && citations[n - h] >= h) h++
return h - 1
}