LeetCode 274
2016-05-20 12:54
344 查看
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.
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-1 ); quick_sort( nums, i+1, right); } } void printfNums( int* nums, int numsSize ) { int i; for( i=0; i<numsSize; i++ ) { printf("%d ", nums[i]); } printf("\n"); } int hIndex(int* citations, int citationsSize) { quick_sort( citations, 0, citationsSize-1 ); int i; for( i=0; i<citationsSize; i++ ) { if( (citationsSize-i) <= citations[i] ) { return citationsSize-i; } } return 0; } int main() { int citations[] = {9,8,7,6,5,4,3,2,1}; int citationsSize = 9; int left = 0; int right = 8; printfNums( citations, citationsSize ); quick_sort( citations, left, right ); printfNums( citations, citationsSize ); int ret = hIndex( citations, citationsSize ); printf("%d\n", ret); return 0; }
相关文章推荐
- 各类编程软件安装配置流程
- 获取系统表和字段
- 手把手教程 Surface如何进行系统恢复?
- 坐标距离算法
- 网络流二十四题之十九 —— 负载平衡问题
- Android 史上最简单的实现Canvas drawText文字垂直居中
- Andriod开发环境搭建
- LeetCode 264
- 程序启动 - 类调用的方法
- 平日小问题(3):分数的求和问题
- [记忆向]工作中一些实用的Linux命令组合(持续)
- Discuz X3.2插件开发(一)
- RecyclerView学习(二)----高仿网易新闻栏目动画效果
- SPOJ_10628_Count_on_a_Tree_(主席树+Tarjan)
- LeetCode 206
- LeetCode 189
- Unity 基础 - Transform
- mysql批量查询、修改表, 批量删除存储过程
- Unity 基础 - Vector3
- LeetCode 142