forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.js
More file actions
24 lines (21 loc) · 585 Bytes
/
res.js
File metadata and controls
24 lines (21 loc) · 585 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @param {number[]} citations
* @return {number}
*/
const hIndex = function(citations) {
const arrLen = citations.length;
const count = new Array(arrLen+1);
for (let index = 0; index < arrLen; index++) {
const element = citations[index];
const realNum = Math.min(element, arrLen);
(count[realNum] ? count[realNum] += 1 : count[realNum] = 1);
}
let sk = count[arrLen] || 0;
let resIndex = arrLen;
while (sk < resIndex) {
resIndex--;
sk += count[resIndex] || 0;
}
// console.log(count);
return resIndex;
};