Leetcode 274: H-Index

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

Note: If there are several possible values for h, the maximum one is taken as the h-index.

 

 1 public class Solution {
 2     public int HIndex(int[] citations) {
 3         if (citations.Length == 0) return 0;
 4         
 5         var h = new int[citations.Length + 1];
 6         
 7         for (int i = 0; i < citations.Length; i++)
 8         {
 9             if (citations[i] >= citations.Length)
10             {
11                 h[citations.Length]++;
12             }
13             else
14             {
15                 h[citations[i]]++;
16             }
17         }
18         
19         int c = 0;
20         for (int i = citations.Length; i >=0; i--)
21         {
22             c += h[i];
23             
24             if (c >= i)
25             {
26                 return i;
27             }
28         }
29         
30         return 0;
31     }
32 }

 

posted @ 2017-12-07 02:40  逸朵  阅读(157)  评论(0)    收藏  举报