Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

274. H-Index #293

Open
Tcdian opened this issue Aug 11, 2020 · 1 comment
Open

274. H-Index #293

Tcdian opened this issue Aug 11, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Aug 11, 2020

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

  • 如果 h 有多有种可能的值 ,h 指数是其中最大的那个。
@Tcdian
Copy link
Owner Author

Tcdian commented Aug 11, 2020

Solution

  • JavaScript 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;
};
  • TypeScript Solution
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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant