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. vs中动态DLL与静态LIB工程中加入版本信息的方法

    说明:本文仅针对刚接触VS不久的新手们(包括ME),提供的一点小Tips,同时也是小生的首篇Blog文章,请大伙多多担待O(∩_∩)O哈! 步骤1 - 在工程中右键添加新建项 步骤2 - 选择创建RC ...

  3. .NET的 DataTable中某列求和

    public DataTable ReportDetail { get; set; }//定义datatable属性 this.txtTotalPiece.Text = ReportDetail.Co ...

  4. ssh localhost无密码登录设置

    亲测... ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys ...

  5. memcmp()直接比较两个数组的大小

    两个字符数组可以用strcmp()比较大小.两个整数数组也有个函数memcmp()可以比较大小,和strcmp()的返回值一样的. 头文件#include<cstring> / #incl ...

  6. add monitor resolution

    cvt 1920 1080 # Get the correct settings for the new mode # Output for me: "1920x1080_60.00&quo ...

  7. 浅谈localStorage本地存储

    在年会的抽奖程序中用到了localStorage现在拿出来总结下,localStorage的相关用法. 在年会抽奖的程序中,需要把获奖名单存储下来,年会现场没有网络,能最简单实现数据存储的方式就是,将 ...

  8. [Head First Python]4.读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out

    datafile.txt  #文件 Man: this is the right room for an argument. Other Man: I've told you once. Man: N ...

  9. Palindrome(POJ 1159 DP)

      Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 58168   Accepted: 20180 De ...

  10. C语言超级经典400道题目

    C语言超级经典400道题目 1.C语言程序的基本单位是____ A) 程序行 B) 语句 C) 函数 D) 字符.C.1 2.C语言程序的三种基本结构是____构A.顺序结构,选择结构,循环结 B.递 ...