2 minute read

Given an array of integers citations where citations[i] is the number of citations a researcher received for their 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

/**
 * @param {number[]} citations
 * @return {number}
 */
// Declare a variable hIndex and assign it a function that takes an array of citations as a parameter
var hIndex = function (citations) {
  // Sort the citations array in descending order
  citations.sort((a, b) => b - a);

  // Initialize a variable hIndexValue to store the calculated h-index, starting at 0
  let hIndexValue = 0;

  // Iterate through the sorted citations array
  for (let i = 0; i < citations.length; i++) {
    // Check if the current citation count is greater than or equal to the current position + 1
    if (citations[i] >= i + 1) {
      // If true, update hIndexValue to the current position + 1
      hIndexValue = i + 1;
    } else {
      // If false, break out of the loop
      break;
    }
  }

  // Return the calculated h-index value
  return hIndexValue;
};

The provided JavaScript code defines a function hIndex that calculates the h-index, a metric measuring a researcher’s impact by balancing the quantity and impact of their work. The function takes an array of paper citation counts as input, sorts it in descending order, and then iterates through the sorted array. For each paper, it compares its citation count with its position in the sorted list. If the citation count is greater than or equal to its position plus one (to account for one-based indexing), it updates the hIndexValue to reflect the current position. The loop continues until a paper no longer meets the condition, at which point it breaks out. The final hIndexValue represents the researcher’s h-index, providing insight into the number of papers and their associated citations that characterize their scholarly impact.

The sorting operation applied to the citations array, which is done in descending order using the sort method. The sorting operation has a time complexity of O(n log n), where n is the number of elements in the array.

Leave a comment