H-Index题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/h-index/description/


Description

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.

Solution

typedef struct range {
int start, end;
} range; range new_range(int s, int e) {
range r;
r.start = s;
r.end = e;
return r;
} void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
} void quickSort(int arr[], int size) {
if (size <= 1)
return; range rangeStack[size], tempRange;
int pos = 0, mid, left, right;
rangeStack[pos++] = new_range(0, size - 1);
while (pos) {
tempRange = rangeStack[--pos];
if (tempRange.start >= tempRange.end)
continue;
left = tempRange.start;
right = tempRange.end - 1;
mid = arr[tempRange.end];
while (left < right) {
while (arr[left] < mid && left < right)
left++;
while (arr[right] >= mid && left < right)
right--;
swap(&arr[left], &arr[right]);
}
if (arr[left] >= arr[tempRange.end])
swap(&arr[left], &arr[tempRange.end]);
else
left++;
rangeStack[pos++] = new_range(tempRange.start, left - 1);
rangeStack[pos++] = new_range(left + 1, tempRange.end);
}
} int hIndex(int* citations, int citationsSize) {
if (citationsSize <= 0)
return 0;
int i;
quickSort(citations, citationsSize);
for (i = citationsSize - 1; i >= 0; i--) {
if (citations[i] >= citationsSize - i) {
if (i == 0) {
return citationsSize;
} else if (citations[i - 1] <= citationsSize - i) {
return citationsSize - i;
}
}
}
return 0;
}

解题描述

这道题我考虑的算法是先将给定的数组进行排序,之后再从后往前检测数组,查看是否存在所求的H指数。我的解答中自己写了用迭代实现的快排

[Leetcode Week4]H-Index的更多相关文章

  1. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  2. LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  3. [LeetCode] Find Pivot Index 寻找中枢点

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  4. LeetCode 852. Peak Index in a Mountain Array C++ 解题报告

    852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...

  5. 【LeetCode】1065. Index Pairs of a String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  6. Leetcode: Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  7. [Leetcode Week4]Merge Two Sorted Lists

    Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...

  8. [Leetcode Week4]Course Schedule II

    Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...

  9. Leetcode 274.H指数

    H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...

随机推荐

  1. 深挖 NGUI 基础 之UIRoot (一)

    当你开始使用NGUI的时候,简单的从项目视图 中一个”Control”预设体 拖拽到场景视图中,你将会发现 Hierarchy层次面板中会出现以下层次结构: 其中 UI Root作为根节点,是每个NG ...

  2. 可以随着SeekBar滑块滑动显示的Demo

    //关于Seek的自定义样式,之前也有总结过,但是,一直做不出随着滑块移动的效果,查询了很多资料终于解决了这个问题,现在把代码写出来有bug的地方 希望大家批评指正. Step 1 :自定义一个Vie ...

  3. npm安装不成功的原因

    使用npm安装electron不成功的解决方法 使用npm安装electron不成功的解决方法 根据官网提供的electron的npm安装方法,使用下面的命令进行安装,结果不成功 npm instal ...

  4. css3弹性盒子模型之box-flex

    css3弹性盒子模型之box-flex 浏览器支持 目前没有浏览器支持 box-flex 属性. Firefox 支持替代的 -moz-box-flex 属性. Safari.Opera 以及 Chr ...

  5. 关于百度Editor富文本编辑器 自定义上传位置

    因为要在网站上编辑富文本数据,所以直接采用百度的富文本编辑器,但是这个编辑器有个缺点,默认情况下,文件只能上传到网站的根目录,不能自定义路径. 而且json配置文件只能和controller.jsp在 ...

  6. 详解python 局部变量与全局变量

    本文将详细分析python的全局变量与局部变量,学过php的人都知道,php里面的全局变量是无法在function里面去使用的,只有超全局变量才可以,那么python会怎么处理全局变量与局部变量呢?下 ...

  7. springmvc文件上传,出现400 的错误问题

    遇见的原因是公司系统上的图片上传忽然不好使了,报错400.单独针对这个模块调了好长时间都没解决,后来才发现前几天做过一个excel上传导入的功能... 使用SptingMVC3.1.3 对于文件上传提 ...

  8. windbg*****************************TBD

    achieve structure from a simple address Dt address know pending IRP in a module !thread xxxxxx到底能提供哪 ...

  9. 【bzoj5070】危险的迷宫 费用流

    题目描述 JudgeOnline/upload/201710/55.doc 输入 第一行是两个整数A与B(1≤A,B≤10),中间用空格分隔,表示该迷宫是A行B列的. 第2行至第A+1行,每行有B个1 ...

  10. How do I see what character set a database / table / column is in MySQL?

    Q: How do I see what the character set that a MySQL database, table and column are in? Is there some ...