LeetCode 274
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.
Hint:
An easy approach is to sort the array first.
What are the possible values of h-index?
A faster approach is to use extra space.
/*************************************************************************
> File Name: LeetCode274.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Thu 19 May 2016 20:47:36 PM CST
************************************************************************/ /************************************************************************* 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. Hint: An easy approach is to sort the array first.
What are the possible values of h-index?
A faster approach is to use extra space. ************************************************************************/ #include "stdio.h" void quick_sort( int* nums, int left, int right )
{
if( left < right )
{
int i = left;
int j = right;
int flag = nums[left]; while( i < j )
{
while( i<j && nums[j]>=flag ) // 从右向左找第一个小于x的数
{
j--;
}
if( i < j )
{
nums[i++] = nums[j];
} while( i<j && nums[i]<flag ) // 从左向右找第一个大于等于x的数
{
i++;
}
if( i < j )
{
nums[j--] = nums[i];
}
}
nums[i] = flag;
quick_sort( nums, left, i- );
quick_sort( nums, i+, right);
}
} void printfNums( int* nums, int numsSize )
{
int i;
for( i=; i<numsSize; i++ )
{
printf("%d ", nums[i]);
}
printf("\n");
} int hIndex(int* citations, int citationsSize)
{ quick_sort( citations, , citationsSize- ); int i;
for( i=; i<citationsSize; i++ )
{
if( (citationsSize-i) <= citations[i] )
{
return citationsSize-i;
}
}
return ;
} int main()
{
int citations[] = {,,,,,,,,};
int citationsSize = ;
int left = ;
int right = ; printfNums( citations, citationsSize );
quick_sort( citations, left, right );
printfNums( citations, citationsSize ); int ret = hIndex( citations, citationsSize );
printf("%d\n", ret); return ;
}
LeetCode 274的更多相关文章
- leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)
https://leetcode.com/problems/h-index/ Given an array of citations (each citation is a non-negative ...
- [LeetCode] 274. H-Index H指数
Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...
- Java实现 LeetCode 274 H指数
274. H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高引用次数"(hig ...
- [LeetCode#274]H-Index
Problem: Given an array of citations (each citation is a non-negative integer) of a researcher, writ ...
- Leetcode 274.H指数
H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- [LeetCode] 275. H-Index II H指数 II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- 【LeetCode】274. H-Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index/ ...
随机推荐
- append some buttons to the standard datagrid pager bar
<script type="text/javascript"> $(function(){ var pager = $('#dg').datagrid('getP ...
- JSF 2 textarea example
In JSF, you can use the <h:inputTextarea /> tag to render a HTML textarea field. For example, ...
- Spring Auto proxy creator example
In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean ...
- 10款无需编程的App DIY开发工具
10款无需编程的App DIY开发工具 你有一个很棒的创意但不会编程怎么办?外包.合伙开发还是从零学编程?这里提供另外一种方式--使用无需编程的App DIY开发工具.DIY开发工具不仅节省了开发时 ...
- presentedViewController 和 presentingViewController 以及 dismissViewControllerAnimated 的使用
在日常的开发中,多控制器之间的跳转除了使用push的方式,还可以使用 present的方式,present控制器时,就避免不了使用 presentedViewController.presenting ...
- 如何将std::string转int,double? (C/C++) (C) (template)
http://www.cnblogs.com/oomusou/archive/2008/08/01/525647.html http://blog.sina.com.cn/s/blog_a843a88 ...
- MATLAB代码
clear;clc%%%%%%%%%%%%方程里的参量%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%alpha=0.5;beta=0.5;%%% ...
- C#关于静态与非静态的区别
C#静态方法与非静态方法的区别不仅仅是概念上的,那么他们有什么具体的区别呢?让我们通过本文向你做一下解析. C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用 ...
- [0.0]Analysis of Baidu search engine
Rencently, my two teammates and I is doing a project, a simplified Chinese search engine for childre ...
- extjs grid renderer用法【转载】
今天在做项目时,需要在列表中的某列添加一个超链接,首先要取得当前选中行的数据,判断数据类型,然后链接到不同的页面,研究下.发现ExtJs提供了一个很强的方法如下: var cm = new Ext.g ...