-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathres.js
44 lines (37 loc) · 790 Bytes
/
res.js
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
37
38
39
40
41
42
43
44
/**
* citations = [0,1,3,5,6]
* index = [1,2,3,4,5]
* @param {*} citations
*/
/**
* @param {number[]} citations
* @return {number}
*/
const hIndex = function(citations) {
const arrLen = citations.length;
let index;
for (index = arrLen-1; index >= 0; index--) {
const element = citations[index];
if (element < (arrLen - index)) {
break;
}
}
return arrLen - index - 1;
};
const hIndex_2 = (citations) => {
const len = citations.length;
if (len == 0 || citations[len - 1] == 0) {
return 0;
}
let left = 0;
let right = len - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (citations[mid] < (len - mid)) {
left = mid + 1;
} else {
right = mid;
}
}
return len - left;
}