Skip to content

Commit 8d36a2f

Browse files
committed
h_index
1 parent a04ebd8 commit 8d36a2f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

include/leeter/h_index.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <vector>
2+
#include <algorithm>
3+
using std::vector;
4+
using std::sort;
5+
6+
namespace h_index {
7+
class Solution {
8+
public:
9+
int hIndex(vector<int>& citations) {
10+
sort(citations.begin(), citations.end());
11+
int n = citations.size();
12+
for (int i = 0; i < n; ++i) {
13+
if (citations[i] >= n - i) {
14+
return n - i;
15+
}
16+
}
17+
return 0;
18+
}
19+
};
20+
} // namespace h_index

test/source/test_h_index.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <doctest/doctest.h>
2+
#include "leeter/h_index.hpp"
3+
4+
5+
TEST_CASE("h index: test case 1") {
6+
vector<int> input = {3, 0, 6, 1, 5};
7+
h_index::Solution sol;
8+
int output = sol.hIndex(input);
9+
CHECK(output == 3);
10+
}
11+
12+
TEST_CASE("h index: test case 2") {
13+
vector<int> input = {1, 3, 1};
14+
h_index::Solution sol;
15+
int output = sol.hIndex(input);
16+
CHECK(output == 1);
17+
}

0 commit comments

Comments
 (0)