LeetCode //274. H-Index

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:

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, their h-index is 3.

Example 2:

Input: citations = [1,3,1]
Output: 1

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;
}

你可能感兴趣的:(LeetCode,leetcode,算法,c语言)