Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Created April 11, 2020 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamkorg/7f9906ae922efd46bdb6b5fb8e47e728 to your computer and use it in GitHub Desktop.
Save adamkorg/7f9906ae922efd46bdb6b5fb8e47e728 to your computer and use it in GitHub Desktop.
Leetcode 275: H-Index II (binary search)
#include <iostream>
#include <vector>
using namespace std;
int hIndex(vector<int>& citations) {
int h=0, len=citations.size();
int l=0, r=len-1, m=l+(r-l)/2;
while (l <= r) {
m = l+(r-l)/2;
if (citations[m] >= len-m){
h = len-m;
r = m-1; //search left half
}
else
l = m+1; //search right half
}
return h;
}
int main() {
vector<int> citations {1}; //{0,1,3,5,6};
cout << hIndex(citations) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment