Created
April 11, 2020 12:23
-
-
Save adamkorg/7f9906ae922efd46bdb6b5fb8e47e728 to your computer and use it in GitHub Desktop.
Leetcode 275: H-Index II (binary search)
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
#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