Tcdian / keep

今天不想做,所以才去做。
MIT License
5 stars 1 forks source link

274. H-Index #293

Open Tcdian opened 3 years ago

Tcdian commented 3 years ago

274. H-Index

给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照 升序排列。编写一个方法,计算出研究者的 h 指数。

h 指数的定义: h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共 有 h 篇论文分别被引用了 至少 h 次。(其余的 N - h 篇论文每篇被引用次数 不超过 h 次。)

Example

Input: citations = [3,0,6,1,5]
Output: 3 
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had 
             received 3, 0, 6, 1, 5 citations respectively. 
             Since the researcher has 3 papers with at least 3 citations each and the remaining 
             two with no more than 3 citations each, her h-index is 3.

Note

Tcdian commented 3 years ago

Solution

/**
 * @param {number[]} citations
 * @return {number}
 */
var hIndex = function(citations) {
    citations.sort((a, b) => a - b);
    let left = 0;
    let right = citations.length - 1;
    while (left <= right) {
        const mid = (left + right) >> 1;
        if (citations[mid] < citations.length - mid) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return citations.length - left;
};
function hIndex(citations: number[]): number {
    citations.sort((a, b) => a - b);
    let left = 0;
    let right = citations.length - 1;
    while (left <= right) {
        const mid = (left + right) >> 1;
        if (citations[mid] < citations.length - mid) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return citations.length - left;
};