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.

  1 /*************************************************************************
  2     > File Name: LeetCode274.c
  3     > Author: Juntaran
  4     > Mail: [email protected]
  5     > Created Time: Thu 19 May 2016 20:47:36 PM CST
  6  ************************************************************************/
  7
  8 /*************************************************************************
  9
 10     H-Index
 11
 12     Given an array of citations (each citation is a non-negative integer)
 13     of a researcher, write a function to compute the researcher‘s h-index.
 14
 15     According to the definition of h-index on Wikipedia:
 16     "A scientist has index h if h of his/her N papers have
 17     at least h citations each, and the other N ? h papers have
 18     no more than h citations each."
 19
 20     For example, given citations = [3, 0, 6, 1, 5],
 21     which means the researcher has 5 papers in total and each of them
 22     had received 3, 0, 6, 1, 5 citations respectively.
 23     Since the researcher has 3 papers with at least 3 citations each
 24     and the remaining two with no more than 3 citations each,
 25     his h-index is 3.
 26
 27     Note:
 28
 29     If there are several possible values for h,
 30     the maximum one is taken as the h-index.
 31
 32     Hint:
 33
 34     An easy approach is to sort the array first.
 35     What are the possible values of h-index?
 36     A faster approach is to use extra space.
 37
 38  ************************************************************************/
 39
 40 #include "stdio.h"
 41
 42 void quick_sort( int* nums, int left, int right )
 43 {
 44     if( left < right )
 45     {
 46         int i = left;
 47         int j = right;
 48         int flag = nums[left];
 49
 50         while( i < j )
 51         {
 52             while( i<j && nums[j]>=flag )    // 从右向左找第一个小于x的数
 53             {
 54                 j--;
 55             }
 56             if( i < j )
 57             {
 58                 nums[i++] = nums[j];
 59             }
 60
 61             while( i<j && nums[i]<flag )    // 从左向右找第一个大于等于x的数
 62             {
 63                 i++;
 64             }
 65             if( i < j )
 66             {
 67                 nums[j--] = nums[i];
 68             }
 69         }
 70         nums[i] = flag;
 71         quick_sort( nums, left, i-1 );
 72         quick_sort( nums, i+1, right);
 73     }
 74 }
 75
 76 void printfNums( int* nums, int numsSize )
 77 {
 78     int i;
 79     for( i=0; i<numsSize; i++ )
 80     {
 81         printf("%d ", nums[i]);
 82     }
 83     printf("\n");
 84 }
 85
 86 int hIndex(int* citations, int citationsSize)
 87 {
 88
 89     quick_sort( citations, 0, citationsSize-1 );
 90
 91     int i;
 92     for( i=0; i<citationsSize; i++ )
 93     {
 94         if( (citationsSize-i) <= citations[i] )
 95         {
 96             return citationsSize-i;
 97         }
 98     }
 99     return 0;
100 }
101
102 int main()
103 {
104     int citations[] = {9,8,7,6,5,4,3,2,1};
105     int citationsSize = 9;
106     int left = 0;
107     int right = 8;
108
109     printfNums( citations, citationsSize );
110     quick_sort( citations, left, right );
111     printfNums( citations, citationsSize );
112
113     int ret = hIndex( citations, citationsSize );
114     printf("%d\n", ret);
115
116     return 0;
117 }
时间: 2024-10-18 01:25:52

LeetCode 274的相关文章

LeetCode 274. H-Index

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

[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] 274. H-Index Java

题目: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 hav

(Java) 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 274.H指数

H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 h 篇论文,分别被引用了至少 h 次,其余的 N - h 篇论文每篇被引用次数不多于 h 次." 示例: 输入: citations = [3,0,6,1,5] 输出: 3 解释: 给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次. 由于研究者有 3 篇论

Leetcode[274]H指数&amp;[904]水果成篮

最近忙于论文和实习,好久没有刷题了,之前立下的关于triplet loss的flag也莫得了. 某公司压力太大,已离职,希望利用好学生生涯最后一点时间,认认真真找个工作,扎扎实实提高自己技术水平. 其实这两道题都特别简单,均是自己在没有提示的情况下做出来的. 第一道题一个排序再一个遍历就结束了,我从没见过这么简单的medium,是因为我的方法太笨了吗? bool compare(const int a, const int b) {return b < a;} class Solution {

[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 your algorithm? Hint: Expected runtime complexity is in O(log n) and the input is sorted. 274. H-Index H指数 的拓展.输入的数组是有序的,让我们优化算法.提示(现在题目中没有提示了):O(logn

[Leetcode] Sort, Hash -- 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 a

LeetCode OJ 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 a