Skip to content

Instantly share code, notes, and snippets.

@ritik-agrawal
Last active May 19, 2023 09:18
Show Gist options
  • Save ritik-agrawal/74df86f545d4d8755f2086fc7450de8c to your computer and use it in GitHub Desktop.
Save ritik-agrawal/74df86f545d4d8755f2086fc7450de8c to your computer and use it in GitHub Desktop.
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;
}
}
@ritik-agrawal
Copy link
Author

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

  • The above code beats 66% of the total solutions submitted in terms of runtime with the runtime as 1ms and beats 35% of total solutions submitted in terms of space complexity.
  • I have implemented quick sort learned previously with pivot as a middle index.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment