274. H-Index
来源:互联网 发布:linux ping命令详解 编辑:程序博客网 时间:2024/06/07 13:32
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.
s思路:
1. 涨指数了,h-index这么计算的。首先想想简单粗暴的计算:排序。例如,[3, 0, 6, 1, 5]递减排序得到[6,5,3,1,0],然后遍历,如果遍历到3,3等于坐标+1,所以继续遍历,1就小于坐标+1,所以返回此时的坐标值。
2. 这样的复杂度就是o(nlgn)。能做到o(n)吗?
3. 看了提示。有o(n)。考虑到h值的边界是0-citations.size();也就是说,h值的范围是confined within size;也就是说,我们可以先扫描一遍,凡是值大于citations.size()的都可以用citations.size()替换,而不影响计算结果;也就是说,现在所有的值也是confined的,妥妥用counting sort就够了,因此复杂度就降低到o(n)。太TM妙了!
4. 找到输出的边界,一切就清楚了!
//方法1:class Solution {public: int hIndex(vector<int>& citations) { // sort(citations.begin(),citations.end(),[](int&a,int&b){ return a>b; }); for(int i=0;i<citations.size();i++){ if(citations[i]<i+1) return i; } return citations.size(); }};//方法2:先对数据做“削顶处理”,先对超出范围的数削顶,然后就是counting sort,美!class Solution {public: int hIndex(vector<int>& citations) { // int n=citations.size(); vector<int> count(n+1,0); for(int i=0;i<citations.size();i++){ if(citations[i]>n){ count[n]++; }else count[citations[i]]++; } int sum=0; for(int i=n;i>=0;i--){ sum+=count[i]; if(sum>=i) return i; } return n; }};
- 274. H-Index & 275. H-Index II
- [leetcode] 274.H-Index
- Leetcode 274. H-Index
- leetcode 274. H-Index
- 274. H-Index
- 274. H-Index
- LeetCode *** 274. H-Index
- 274. H-Index
- 274. H-Index
- 274. H-Index
- 274. H-Index
- leetcode.274. H-Index
- 【leetcode】274. H-Index
- 274. H-Index
- 274. H-Index
- 274. H-Index
- LeetCode - 274. H-Index
- 274. H-Index
- 我的数学知识体系
- MySQL性能调优my.cnf详解https://blog.linuxeye.com/379.html
- MU-MIMO技术让网络变神速的方法
- 细说 C++ Traits Classes
- Using 的用法
- 274. H-Index
- NHibernate之旅(1):开篇有益
- NHibernate之旅(2):第一个NHibernate程序
- 50道Java线程面试题
- Python中字典的定义与简单使用实例
- 20170213心情总结
- virtualenv介绍及基本使用
- 有趣的机器学习(六)
- WSAEWOULDBLOCK: Resource temporarily unavailable