当前位置: 面试刷题>> H 指数(经典算法150题)


题目描述补充

题目:H指数(H-Index)

H指数是一个用于评价学者或研究者学术成就的指标,特别是在科学界和学术界广泛使用。一个研究者的H指数是指他/她至多有h篇论文分别被引用了至少h次,而其余论文的引用次数则可能低于h次。换句话说,如果某位研究者有h篇论文,每篇论文的引用次数都至少为h,且没有(h+1)篇论文的引用次数都至少为(h+1),那么这位研究者的H指数就是h。

任务: 给定一个包含论文引用次数的数组(每个元素代表一篇论文的引用次数),编写一个函数来计算并返回该数组的H指数。

示例

假设输入数组为 [3, 0, 6, 1, 5],那么H指数为2,因为有2篇论文(引用次数为6和5的论文)至少被引用了2次,而其他论文的引用次数则不足2次。

PHP代码示例

function calculateHIndex($citations) {
    rsort($citations); // 对引用次数数组进行降序排序
    $hIndex = 0;
    foreach ($citations as $index => $citation) {
        if ($citation >= $index + 1) {
            $hIndex = $index + 1;
        } else {
            break;
        }
    }
    return $hIndex;
}

// 示例用法
$citations = [3, 0, 6, 1, 5];
echo calculateHIndex($citations); // 输出: 2

Python代码示例

def calculate_h_index(citations):
    citations.sort(reverse=True) # 对引用次数数组进行降序排序
    h_index = 0
    for i, citation in enumerate(citations):
        if citation >= i + 1:
            h_index = i + 1
        else:
            break
    return h_index

# 示例用法
citations = [3, 0, 6, 1, 5]
print(calculate_h_index(citations)) # 输出: 2

JavaScript代码示例

function calculateHIndex(citations) {
    citations.sort((a, b) => b - a); // 对引用次数数组进行降序排序
    let hIndex = 0;
    for (let i = 0; i < citations.length; i++) {
        if (citations[i] >= i + 1) {
            hIndex = i + 1;
        } else {
            break;
        }
    }
    return hIndex;
}

// 示例用法
const citations = [3, 0, 6, 1, 5];
console.log(calculateHIndex(citations)); // 输出: 2

在这些示例中,我们首先对数组进行降序排序,然后遍历数组,检查每篇论文的引用次数是否满足H指数的定义。一旦不满足条件,就终止循环并返回当前的H指数。这种方法的时间复杂度主要由排序决定,为O(n log n),其中n是数组的长度。

推荐面试题