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

275. H-Index II #220

Open
Tcdian opened this issue Jun 18, 2020 · 1 comment
Open

275. H-Index II #220

Tcdian opened this issue Jun 18, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 18, 2020

275. H-Index II

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

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

Example

Input: citations = [0,1,3,5,6]
Output: 3 
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had 
             received 0, 1, 3, 5, 6 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 Jun 18, 2020

Solution

  • JavaScript Solution
/**
 * @param {number[]} citations
 * @return {number}
 */
var hIndex = function(citations) {
    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 {
    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