1: class Solution {
2: public:
3: int hIndex(vector<int>& citations) {
4: sort(citations.begin(), citations.end());
5: int h = 0, n = citations.size();
6: for (int i = 0; i < n; i++) {
7: if (citations[i] >= n-i) {
8: h = n-i;
9: break;
10: }
11: }
12: return h;
13: }
14: };
When I revisited this problem, I found a better explanation of the algorithm. If you sort the citations and put it into a graph, where x-axis represents the paper index and y-axis represents the citations. So, from the graph, the H-Index is length of the maximum square that in the histogram.
No comments:
Post a Comment