forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh-Index_II.cpp
More file actions
36 lines (32 loc) · 1.15 KB
/
h-Index_II.cpp
File metadata and controls
36 lines (32 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Source : https://leetcode.com/problems/h-index-ii/
// Author : Calinescu Valentin
// Date : 2015-10-23
/***************************************************************************************
*
* Follow up for H-Index: What if the citations array is sorted in ascending order?
* Could you optimize your algorithm?
*
***************************************************************************************/
/*
* Solutions
* =========
*
* At every step we need to check whether this element is not less than
* the remaining number of elements bigger than it(including itself) and all the values of
* the other elements smaller than it are not more than that number. The h_index is this
* number of elements bigger than it(including itself).
*
* Time Complexity: O(N)
* Space Complexity: O(1)
*
*/
class Solution {
public:
int hIndex(vector<int>& citations) {
int h_index = 0;
for(int i = citations.size() - 1; i >= 0; i--)
if(citations[i] >= citations.size() - i && (i - 1 < 0 || citations[i - 1] <= citations.size() - i))
h_index = citations.size() - i;
return h_index;
}
};