H-index II
Follow up to H-index:what if the citations
array is sorted in ascending order? Could you optimize your algorithm?
Hint:
- Expected runtime complexity is on O(log n) and the input is sorted.
https://leetcode.com/problems/h-index-ii/
Immediately before the previous question: http://www.cnblogs.com/Liok3187/p/4782658.html
The given array is ascending, requiring a time complexity of O (Logn) and dichotomy.
H stands for "high citation" (Hi citations), a researcher's H index refers to at most H-papers which are quoted at least H times respectively.
To determine a person's H index is very easy, to the SCI site, to find out a person published all SCI papers, let it ranked from high to low by the cited number, check down, until the number of a paper is more than the number of citations, the number minus 1 is the H index.
Or to find the inflection point, take the data of a question example: [0, 1, 3, 5, 6]
Len-index |
Index |
Citations[index] |
5 |
0 |
0 |
4 |
1 |
1 |
3 |
2 |
3 |
2 |
3 |
5 |
1 |
4 |
6 |
Index is the subscript of the array, minus index with the length len of the array is the number we need to compare the ordinal with the corresponding value.
If Citations[index] >= Len-index, the result is in the first half of the array.
Otherwise citations[index] < Len-index, two cases.
1. The latter paper, Citations[index + 1] >= len-(index + 1), indicates that the inflection point was found and the output was obtained.
2. The result is in the second half of the array.
Finally, consider a special case, such as [5,6,7].
After two minutes to find the results, because each paper cited more than the number of times, the direct output len can be, "H-papers were quoted at least H", more than H or H.
2 * @param {number[]} citations3 * @return {number}4 */5 varHindex =function(citations) {6 varLen =citations.length;7 varStart = 0, end =len, index;8 while(Start <=end) {9index = parseint (start + end)/2);Ten if(Citations[index] = = =undefined) { One return0; A } - if(Citations[index] >= len-index) { -End = Index-1; the}Else{ - if(Citations[index + 1] >= len-index-1){ - returnLen-index-1; -}Else{ +Start = index + 1; - } + } A } at returnLen; -};
[Leetcode][javascript]h-index II