Skip to content

Commit

Permalink
Update h-index.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Sep 4, 2015
1 parent 4afbe68 commit e67c705
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions C++/h-index.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
// Time: O(nlogn)
// Space: O(1)
// Time: O(n)
// Space: O(n)

// Counting sort.
class Solution {
public:
int hIndex(vector<int>& citations) {
const auto n = citations.size();
vector<int> count(n + 1, 0);
for (const auto& x : citations) {
if(x >= n) {
++count[n];
} else {
++count[x];
}
}

int h = 0;
for (int i = n; i >= 0; --i) {
h += count[i];
if (h >= i) {
return i;
}
}
return h;
}
};

// Time: O(nlogn)
// Space: O(1)
class Solution2 {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(), greater<int>());
Expand Down

0 comments on commit e67c705

Please sign in to comment.