Skip to content

Commit 653b2a4

Browse files
committed
Add solution #275
1 parent 0f07030 commit 653b2a4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
268|[Missing Number](./0268-missing-number.js)|Easy|
220220
273|[Integer to English Words](./0273-integer-to-english-words.js)|Hard|
221221
274|[H-Index](./0274-h-index.js)|Medium|
222+
275|[H-Index II](./0275-h-index-ii.js)|Medium|
222223
278|[First Bad Version](./0278-first-bad-version.js)|Medium|
223224
282|[Expression Add Operators](./0282-expression-add-operators.js)|Hard|
224225
283|[Move Zeroes](./0283-move-zeroes.js)|Easy|

solutions/0275-h-index-ii.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 275. H-Index II
3+
* https://leetcode.com/problems/h-index-ii/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers citations where citations[i] is the number of citations a
7+
* researcher received for their ith paper and citations is sorted in ascending order,
8+
* return the researcher's h-index.
9+
*
10+
* According to the definition of h-index on Wikipedia: The h-index is defined as the
11+
* maximum value of h such that the given researcher has published at least h papers
12+
* that have each been cited at least h times.
13+
*
14+
* You must write an algorithm that runs in logarithmic time.
15+
*/
16+
17+
/**
18+
* @param {number[]} citations
19+
* @return {number}
20+
*/
21+
var hIndex = function(citations) {
22+
let start = 0;
23+
24+
for (let end = citations.length - 1; start <= end;) {
25+
const middle = Math.floor((start + end) / 2);
26+
if (citations.length - middle - 1 < citations[middle]) {
27+
end = middle - 1;
28+
} else {
29+
start = middle + 1;
30+
}
31+
}
32+
33+
return citations.length - start;
34+
};

0 commit comments

Comments
 (0)