題目
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N ? h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
- This is a follow up problem to H-Index, where
citationsis now guaranteed to be sorted in ascending order. - Could you solve it in logarithmic time complexity?
題意
設一個研究者共有n篇論文,如果其中有h篇,這h篇中每一篇都被參考過至少h次,而剩余的n-h篇中每一篇被參考的次數都不超過h,則稱h為這個研究者的h指數,給定一個研究者n篇論文參考次數的升序陣列,求該研究者h指數的最大值,
思路
與 0274. H-Index 相比,已經將陣列按照升序排列,這樣反而更簡單,直接在 0274 的基礎上使用二分查找進行改進即可,
代碼實作
Java
class Solution {
public int hIndex(int[] citations) {
int left = 0, right = citations.length - 1;
while (left < right) {
int mid = (right - left) / 2 + left;
if (citations[mid] >= citations.length - mid) {
right = mid;
} else {
left = mid + 1;
}
}
return left == right && citations[left] >= citations.length - left ? citations.length - left : 0;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/38860.html
標籤:其他
