0
点赞
收藏
分享

微信扫一扫

LeetCode·每日一题·1851. 包含每个查询的最小区间·优先队列(小顶堆)

敬亭阁主 2023-07-24 阅读 67

274. H-Index

Given an array of integers citations where citations[i] is the number of citations a researcher received for their i t h i^{th} ith paper, return the researcher’s h-index.

According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.

 

Example 1:

Example 2:

Constraints:

  • n == citations.length
  • 1 <= n <= 5000
  • 0 <= citations[i] <= 1000

From: LeetCode
Link: 274. H-Index


Solution:

Ideas:
The code calculates the h-index of a researcher given the number of citations for each of their papers. Here’s a step-by-step explanation of the code:
1. Bubble Sort: The first part of the code sorts the array of citations in descending order using the Bubble Sort algorithm. In Bubble Sort, we repeatedly iterate through the list, compare adjacent elements and swap them if they are in the wrong order. The iteration is repeated until no more swaps are needed, indicating that the list is sorted. Here, we want to sort in descending order, so we swap whenever the current element is less than the next one.
2. Calculating h-index: After sorting the citations, we calculate the h-index. The h-index is defined as the number of papers with citation greater than or equal to the number of papers. We start from the paper with the highest citation, and keep moving to the paper with the next highest citation, increasing the count of papers as we go. As soon as we reach a paper where the count of papers so far is greater than the citation of the current paper, we stop. The count of papers at this point is the h-index.
In terms of code:
  • We initialize h_index as 0.
  • Then, we start a for loop where i goes from 0 to citationsSize - 1. At each iteration, we check if i is greater than or equal to citations[i]. This is because i is the number of papers that have a citation count of at least citations[i] (as the array is sorted in descending order).
  • If i is greater than or equal to citations[i], we break the loop, because we have found the point where the number of papers is greater than the citation count of the current paper.
  • If i is less than citations[i], we increment h_index by 1.
  • At the end of the function, we return h_index as the calculated h-index.
This code adheres to the definition of h-index: A scientist has index h if h of their N papers have at least h citations each, and the other (N − h) papers have no more than h citations each.
Code:
int hIndex(int* citations, int citationsSize){
    // Bubble sort in descending order
    for (int i = 0; i < citationsSize-1; i++) {
        for (int j = 0; j < citationsSize-i-1; j++) {
            if (citations[j] < citations[j+1]) {
                int temp = citations[j];
                citations[j] = citations[j+1];
                citations[j+1] = temp;
            }
        }
    }

    int h_index = 0;
    for (int i = 0; i < citationsSize; i++) {
        if (i >= citations[i]) {
            break;
        }
        h_index = i + 1;
    }
    return h_index;
}
举报

相关推荐

0 条评论