Leetcode Note: H-Index

Source: Internet
Author: User

Leetcode Note: H-Index

I. 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, givencitations = [3, 0, 6, 1, 5], Which means the researcher has 5 papers in total and each of them had stored ed3, 0, 6, 1, 5Citations 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.

Ii. Question Analysis

First, you need to understand the general idea of the question:

Given an array that records the number of times a researcher references a document (each document references a non-negative integer), write a function to calculate the h index of the investigator.

According to Wikipedia's definition of the h index: "A scientist's h index is published inNIn this paperhThe papers are referenced at leasthTimes, OtherN-hNo morehTimes ".

For example, given an arraycitations = [3, 0, 6, 1, 5], Which means that the researchers have a total5Each article has obtained3, 0, 6, 1, 5. Since researchers have3The papers have at least obtained3The number of references in the other two articles does not exceed3Times, so its h index is3.

NOTE: If there are multiple possiblehValue, take the maximum valuehIndex.

You can learn morehThe definition of the value, which is in the lower left corner of the ball.SquareMaximum:

In the following explanation, assume that the size of the given array isN, That is, the totalNArticle.

There are two common practices, which are also mentioned in the topic tips. The first thing we think of is to sort the array and traverse from the back to find out the h value. The complexity of this method is:O(n*logn).

During the interview, if secondary memory is allowed, you can use the second method, that is, opening up a new arrayrecordFor record0~NThe number of times each times has several articles.NAccordingNCount) traverse the array. After counting, traverse the array once.RecZ records? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcmQ8L2NvZGU + o6y8tL/Jy + weight + yP0uIMq + wP20 + sLrPC9zdHJvbmc + Weight =" brush: java; ">// Sort + traverse class Solution {public: int hIndex (vector & Citations) {sort (citations. begin (), citations. end (), [] (const int & a, const int & B) {return a> B ;}); int I = 0; for (; I <citations. size (); ++ I) if (citations [I] <= I) break; return I ;}};

// Method 2 class Solution {public: int hIndex (vector  & Citations) {int citationSize = citations. size (); if (citationSize <1) return 0; vector  Record (citationSize + 1, 0); for (int I = 0; I <citationSize; ++ I) {if (citations [I] <= citationSize) ++ record [citations [I]; else ++ record [citationSize];} for (int j = citationSize, paperNum = 0; j> = 0; -- j) {paperNum + = record [j]; if (paperNum> = j) return j;} return 0 ;}};  

Iv. Summary

The method used depends on the actual conditions.

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.