leetcode笔记:H-Index

一. 题目描述

Given an array of citations (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.”

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 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, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

二. 题目分析

首先需要了解一下题目的大意:

给定一个数组,记载了某研究人员的文章引用次数(每篇文章的引用次数都是非负整数),编写函数计算该研究人员的h指数。

根据维基百科上对h指数的定义:“一名科学家的h指数是指在其发表的N篇论文中,有h篇论文分别被引用了至少h次,其余N-h篇的引用次数均不超过h次”。

例如,给定一个数组citations = [3, 0, 6, 1, 5],这意味着该研究人员总共有5篇论文,每篇分别获得了3, 0, 6, 1, 5次引用。由于研究人员有3篇论文分别至少获得了3次引用,其余两篇的引用次数均不超过3次,因而其h指数是3

注意:如果存在多个可能的h值,取最大值作为h指数。

通过下图,可以更直观了解h值的定义,对应图中,即是球左下角正方形的最大值:

以下解释中,假设给定数组的大小为N,即共有N篇文章。

常规的做法有两种,也是题目tips中提到的,首先想到的是将数组进行排序,然后从后往前遍历,找出这个h值,该方法的复杂度是:O(n*logn)

在面试中,若允许使用辅助内存,可以使用第二种方法,即开辟一个新数组record,用于记录0~N次引用次数的各有几篇文章(引用次数大于N的按照N次计算)遍历数组,统计过后,遍历一次统计数组record,即可算出h值的最大值。时间复杂度为O(n)

三. 示例代码

// 排序+遍历
class Solution {
public:
    int hIndex(vector<int>& citations) {
        sort(citations.begin(), citations.end(), [](const int &a, const int &b){return a > b; });
        int i = 0;
        for (; i < citations.size(); ++i)
            if (citations[i] <= i)
                break;
        return i;
    }
};
// 第二种的方法
class Solution {
public:
    int hIndex(vector<int>& citations) {
        int citationSize = citations.size();
        if (citationSize < 1) return 0;
        vector<int> record(citationSize + 1, 0);
        for (int i = 0; i < citationSize; ++i)
        {
            if (citations[i] <= citationSize)
                ++record[citations[i]];
            else
                ++record[citationSize];
        }

        for (int j = citationSize, paperNum = 0; j >= 0; --j)
        {
            paperNum += record[j];
            if (paperNum >= j) return j;
        }
        return 0;
    }
};

四. 小结

使用何种方法,需要根据实际条件而定。

时间: 2024-10-09 06:57:25

leetcode笔记:H-Index的相关文章

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

leetcode笔记:Pascal&amp;#39;s Triangle

一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 二. 题目分析 关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

leetcode笔记:ZigZag Conversion

一. 题目描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PA

leetcode笔记11 First Unique Character in a String

题目描述: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only low

leetcode笔记:Remove Duplicates from Sorted Array

一.题目描述 二.解题技巧 从题目中可知,数组中的元素事先已经过排序,因此一个简单而易于实现的方法是从第二个元素开始对数组元素进行遍历,并判断该元素是否和前面的元素相同.题目要求返回不重复的元素的个数. 算法的一个要求是:返回的数组的元素都是不重复,同时要求remove duplicates in place,这意味着不能重新定义一个数组来保存不重复的元素.假设该数组为A,为了满足这一要求,可以设置一个变量num来保存不重复的元素的个数,然后遍历整个数组A,如果该元素A[i]!=A[i-1]的话

leetcode笔记—String

3. Longest Substring Without Repeating Characters (medium) 最长的无重复的子字符串 Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given

leetcode笔记:Find Median from Data Stream

一. 题目描述 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 De