# Share my c++ code ( descending order sort )

• ``````class Solution
{
public:
static bool cmp(int n, int m)
{
//true : n is bigger
//false: m is bigger

if(n>m)
return true;
else
return false;
}
int hIndex(vector<int>& citations)
{
if(citations.size() == 0)
return 0;

size_t i = 0;
sort( citations.begin(), citations.end(),cmp ); // descending order

for(i=0;i<citations.size();i++)
{
if( i >= citations[i] )
{
return ( i );
}
}
return i;
}
};``````

• Hi,

I came across a situation: given citations = [3, 1, 7, 8, 9]. What will be the answer be?

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."

When h=3, the definition will be "A scientist has index 3 if 3 of his/her 5 papers have at least 3 citations each, and the other 2 papers have no more than 3 citations each." Obviously wrong....

When h=4, the definition will be "A scientist has index 4 if 4 of his/her 5 papers have at least 4 citations each, and the other 1 papers have no more than 4 citations each." Obviously wrong....

When h=2, the definition will be "A scientist has index 2 if 2 of his/her 5 papers have at least 2 citations each, and the other 3 papers have no more than 3 citations each." Obviously wrong....

• Hello~

First.
Introduce my idea for your data set.

[ 3, 1, 7, 8, 9 ] <= is raw data ,before descending sort...

after descending order sort
=>
index 1 2 3 4 5
data 9 8 7 3 1

and
index '3' is h index .

reason is
index ' 1,2,3 ' is lower then data '9,8,7'

but
index '4' ,is higher than data '3'

so
index '3' is 'h_index'

is the my idea.

Second.

I'm not very well know about the H_index,
so I found another introductions of H_index .

here is
2 check point.

if index and citation is same
then index is H_index

but
if citation is lower then index(or index is higher than citations)
then previous index is(index - 1)
the 'H index'.

so my algorithm is need descending order sort.(for ranking)

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.