Last active
May 19, 2023 09:18
-
-
Save ritik-agrawal/74df86f545d4d8755f2086fc7450de8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int hIndex(int[] citations) { | |
var len = citations.length; | |
if (len == 1){ | |
if (citations[0] == 0){ | |
return 0; | |
} else { | |
return 1; | |
} | |
} | |
sort(citations,0 ,(len-1)); | |
//displayArray(citations); | |
var cur = len -1; | |
while(cur > 0 && citations[cur] < cur){ | |
cur--; | |
} | |
var ret = citations[cur] > (cur+1) ? (cur+1) : citations[cur]; | |
return ret; | |
} | |
//The below code is separate from the problem and is used to sort the given integer array. | |
//Quick sort is used with mid element as the pivot to sort in decending order. | |
private void sort(int[] ar, int l, int h){ | |
if (l < h){ | |
var pivot = partision(ar, l, h); | |
sort(ar, l, (pivot - 1)); | |
sort(ar, (pivot + 1), h); | |
} | |
} | |
private int partision(int[] ar, int l, int h){ | |
int m = (l+h) / 2; | |
var pivot = ar[m]; | |
var marker = (l-1); | |
for(int cur = l; cur <= h; cur++){ | |
if(ar[cur] > pivot){ | |
marker++; | |
swap(ar, marker, cur); | |
if (marker == m){ | |
m = cur; | |
} | |
} | |
} | |
swap(ar, (marker+1), m); | |
return (marker+1); | |
} | |
private void swap(int[] ar, int a, int b){ | |
var tmp = ar[a]; | |
ar[a] = ar[b]; | |
ar[b] = tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leet Code
Given an integer array where the ith index has the count of citations on the ith paper of an author, we need to get the H-index.
Achievement
H-Index
The h-index is a metric to rate an author's success based on the count of citations on the papers published by him/her.
Link