1.H-Index
Total Accepted: 19058 Total Submissions: 70245 Difficulty: Medium

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.

 
1.如果有序,题目的意思是, 
h = n-i;
citations[i] >= h 就符合要求
/*
0 1 4 5 6 7 9
*/ class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
sort(citations.begin(),citations.end()) ;
for(int i=;i<n;i++){
if(citations[i] >= n-i) return n-i;
}
return ;
}
};

2.也可使用二分的思路来做,因为我们知道h的范围是[0,h],而且每一个h都有固定的规则,那么就可以用二分来逼近答案

low=,high=n+;

while(low<high)
{
h=low+(high-low)/;
统计citation>h的数量为y
如果有y=10篇大于等于h=citaton=8的文章,说明h不符合要求,也就是说我们选的citation太小了,我们把citation上移一个,即low=mid+;
如果有y=4篇大于等于h=ciation=8的文章,转换一下就是所,有4篇papers的cication>=,也就是我们选的citation太大了,把citation下移一个,high = mid-;
如果相等,那么找到了
}
return low-
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
if(n==){
return ;
}
int low = ;
int high= n+;
while(low<high){
int h = low+(high-low)/;
int y = ;
for(int i=;i<n;i++){
if(citations[i] >= h) y++;
}
if(y == h){
return h;
}else if(y<h){
high = h;
}else{
low = h+;
}
}
return low-;
}
};

 2.H-Index II

Total Accepted: 13860 Total Submissions: 43755 Difficulty: Medium

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
int low_index = ;
int high_index= n-;
while(low_index<=high_index){
int y = low_index+(high_index-low_index)/;
int h = n-y;
if(citations[y] == h){
return h;
}else if(citations[y] < h){
low_index = y+;
}else{
high_index = y-;
}
}
return n== ? : n-low_index;
}
};

H-Index,H-Index II的更多相关文章

  1. C 缓冲区过读 if (index >= 0 && index < len)

    C 缓冲区过读 if (index >= 0 && index < len) CWE - CWE-126: Buffer Over-read (3.2) http://cw ...

  2. index index.html index.htm index.php

    server { listen 80; server_name localhost; index index.html index.htm index.php;#前后顺序有关系,越在前优先级别越高 r ...

  3. nginx -t "nginx: [warn] only the last index in "index" directive should be absolute in 6 "的问题解决

    修改完nginx的配置文件之后,执行nginx -t命令提示"nginx: [warn] only the last index in "index" directive ...

  4. 14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构

    14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构 所有的InnoDB indexes 是 B-trees Index r ...

  5. 14.2.5.4 Physical Structure of an InnoDB Index InnoDB Index 的物理结构

    14.2.5.4 Physical Structure of an InnoDB Index InnoDB Index 的物理结构 所有的InnoDB indexes 是B-trees ,index ...

  6. MySQL 执行计划中Extra(Using where,Using index,Using index condition,Using index,Using where)的浅析

      关于如何理解MySQL执行计划中Extra列的Using where.Using Index.Using index condition,Using index,Using where这四者的区别 ...

  7. [Oacle][Partition]Partition操作与 Index, Global Index 的关系

    [Oacle][Partition]Partition操作与 Index, Global Index 的关系: ■ Regarding the local index and the global i ...

  8. Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...

  9. 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...

  10. thinkphp报错Call to undefined method app\index\controller\Index::fetch()

    因为要写一个系统,所以又重新下载了thinkphp,然后安装了一下.回忆起这个问题很容易让新手朋友费解.会出现如下报错:Call to undefined method app\index\contr ...

随机推荐

  1. 关于arm-linux-gcc的安装与配置

    在嵌入式开发中我们经常会用到arm-linux-gcc来编译我们的应用程序.作为arm-linux-gcc的入门,我们先看看如何安装arm-linux-gcc. 安装arm-linux-gcc还是比较 ...

  2. XtraReport改变纸张方向

    XtraReport纸张方向改变可以通过修改Landscape属性: Landscape=true 为横向输出 Landscape=false 为纵向输出

  3. OpenSSl编译

    1.下载openssl代码,下载地址:http://www.openssl.org/source/ ,如果使用winrar解压失败的话(提示不能创建符号链接),可以关闭UAC.2.下载安装Active ...

  4. (原)使用vectot的.end()报错:iterators incompatible

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5070672.html 参考网址: http://blog.csdn.net/yxnyxnyxnyxny ...

  5. 查找mysql数据库文件的存放位置

    在mysql数据库中,有时候并不是很容易找出mysql数据库文件data的存放位置吗,这时就可以使用mysql自带的命令行工具进行查询. 具体命令如下:show variables like '%da ...

  6. HTML5简单入门系列(八)

    前言 本篇介绍HTML5中相对复杂的一些APIs,其中的数学知识比较多.虽然如此,但是其实API使用起来还是比较方便的. 这里说明一下,只写出API相关的JS代码,因为他们都是基于一个canvas标签 ...

  7. Python之路第十二天,高级(5)-Python操作Mysql,SqlAlchemy

    Mysql基础 一.安装 Windows: 1.下载 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.31-winx64.zip 2.解压 ...

  8. project euler 25 fibonacci

    数学方法: Saying that a number contains 1000 digits is the same as saying that it's greater than 10**999 ...

  9. STC10F10XE定时器中断输出10KHz的方波程序

    //咱做硬件的也动手做点测试程序,为了测试新做的电机驱动板,找了个51的板子当10K信号发生器测试IGBT开关延时时间. #include <STC_NEW_8051.H>#include ...

  10. Linux下GDB调试C/C++

    首先先编译程序并生成调试符号: gcc -g -c main.cpp gcc -o exefile main.o 以上的exefile为可执行程序的文件名 然后: gdb exefile 可以开始gd ...