LeetCode -- H-Index
Description:
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N? H papers have no more than h citations each ."
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had encoded ed 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, his h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
For each number arr [I] in an array arr, where I in [0, n-1], find the number of elements k, where 0
Ideas:
For array arr, if it is sorted in reverse order and then traversed, for the relationship between I and arr [I], because I is increasing, while arr [I] is decreasing, as long as the first I> = arr [I] is found, the nth-I element must satisfy I> = arr [I]. Therefore, in the traversal process, as long as all
Therefore, this question can be converted to: sort the array arr in reverse order and count the number of indexes <arr [index. For example
For, after sorting :,
0 <7 => count ++
1 <5 => count ++
2 <3 => count ++
3 = 3
4> 2
5> 1
Therefore, H-index is count = 3.
Implementation Code:
public class Solution { public int HIndex(int[] citations) { if(citations.Length == 0){ return 0; } var sorted = citations.OrderByDescending(x=>x).ToList(); var sum = 0; for(var i = 0;i < sorted.Count; i++){ if(i < sorted[i]){ sum ++; } } return sum; }}