[LeetCode] H-Index

If you‘ve read the Wikipedia article of H-Index, there is already a neat formula there for computing the h-index, which is written below using the notations of the problem. Note that in the formula below, citations is sorted in descending order and i is 1-indexed.

h = max_i(min(i, citations[i]))

Now you will easily write down the following code.

 1 class Solution {
 2 public:
 3     int hIndex(vector<int>& citations) {
 4         sort(citations.rbegin(), citations.rend());
 5         int h = 0, i = 0;
 6         for (int c : citations)
 7             h = max(h, min(++i, c));
 8         return h;
 9     }
10 };

This code takes 20ms. In fact, rbegin and rend seems to be relatively slow. An alternative is to sort citations in normal ascending order and then count all those papers with citations larger than their indexes, as what Stefan does here. Now the code runs in 12ms.

 1 class Solution {
 2 public:
 3     int hIndex(vector<int>& citations) {
 4         sort(citations.begin(), citations.end());
 5         int h = 0, i = citations.size();
 6         for (int c : citations)
 7             h += (c > --i);
 8         return h;
 9     }
10 };

Well, both the above codes are in O(nlogn) time. Is there a linear time solution? The answer is yes: refer to this post if you like :-)

 1 class Solution {
 2 public:
 3     int hIndex(vector<int>& citations) {
 4         int n = citations.size(), h = 0;
 5         int* counts = new int[n + 1]();
 6         for (int c : citations)
 7             counts[min(c, n)]++;
 8         for (int i = n; i; i--) {
 9             h += counts[i];
10             if (h >= i) return i;
11         }
12         return h;
13     }
14 };

This code uses both linear time and space, and runs in 8ms. Wow, we‘ve moved a long way from the original 20ms version :-)

时间: 2024-08-08 01:50:43

[LeetCode] H-Index的相关文章

LeetCode Minimum Index Sum of Two Lists

原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find

[LeetCode] Minimum Index Sum of Two Lists 两个链表的最小序列和

Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with the least list index sum. If there is a choice tie betw

[LeetCode] 274. H-Index H指数

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 a

leetcode -eleven:Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

H.264解码过程剖析-4

x264开源工程实现H.264的视频编码,但没有提供对应的解码器.ffmpeg开源多媒体编解码集合汇集了市面上几乎所有媒体格式的编解码的源代码.其中的H264.c就是一个能正常解码x264编码码流的独立的源文件,其使用步骤也与上述的编码或解码CODEC应用案例基本相同.这一节通过自顶向下的方式,讲述H264.c如何实现H.264视频解码过程. H264.c源文件有几千行,代码量庞大,很不便于浏览.分析和移植.同时该文件还依赖其他源文件,组织结构较复杂,实现平台由于不是基于Windows的VC++

【leetcode】Maximal Rectangle

Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 使用dpHeight[]数组来记录到第i行为止,第j个位置垂直连续包含多少个1(包括matrxi[i][j]).如: 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 有如下结果: 第1行: dpHeight[] =

FFmpeg的H.264解码器源代码简单分析:熵解码(Entropy Decoding)部分

本文分析FFmpeg的H.264解码器的熵解码(Entropy Decoding)部分的源代码.FFmpeg的H.264解码器调用decode_slice()函数完成了解码工作.这些解码工作可以大体上分为3个步骤:熵解码,宏块解码以及环路滤波.本文分析这3个步骤中的第1个步骤. 函数调用关系图 熵解码(Entropy Decoding)部分的源代码在整个H.264解码器中的位置如下图所示. 单击查看更清晰的图片 熵解码(Entropy Decoding)部分的源代码的调用关系如下图所示. 单击查

LeetCode设计实现题(一)

一.LRU缓存机制(LeetCode-146) 1.1 题目描述 1.2 解题思路 思路1: 使用Map存放key,value,使用List存放key和count,count为最新的index值,每次put.get操作都会使index自增. 进行put操作时,如果发现超过容量值capacity,则对list中的count排序,map和list都删除掉index最小的元素.(提示超时) 思路2: 使用LinkedList,每次put操作或get操作,当list中没有该key的元素的时候,且不超过容

[leetcode] 树(Ⅰ)

均为 Simple 难度的水题. 二叉树的中序遍历 题目[94]:给定一个二叉树,返回它的中序 遍历. 解题思路:Too simple. class Solution { public: vector<int> inorderTraversal(TreeNode *root) { return inorderNonRec(root); vector<int> v; innerTraversal(root, v); return v; } void innerTraversal(Tr

uva10282-字典

此题为小白书暴力求解法哈希表的训练参考 题目链接 http://acm.hust.edu.cn/vjudge/problem/24031 最近为了快速入门...所以挑了些简单的题目先做... 解题思路 最多会录入100000个单词对.可以设计一个简单的哈希函数,比如下标*单词这样的, 当然这样会造成较多冲突,如果是极限数据的话装填因子a达到了100... 如果用拉链法解决冲突的话,平均查找长度其查找成功大概是50左右,速度上还是可以凑合的. 填装因子a = 填入表中记录的个数 / 散列表的长度