LeetCode -- H-Index

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.