H-Index,H-Index II
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.
/*
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
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的更多相关文章
- C 缓冲区过读 if (index >= 0 && index < len)
C 缓冲区过读 if (index >= 0 && index < len) CWE - CWE-126: Buffer Over-read (3.2) http://cw ...
- index index.html index.htm index.php
server { listen 80; server_name localhost; index index.html index.htm index.php;#前后顺序有关系,越在前优先级别越高 r ...
- 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 ...
- 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 ...
- 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 ...
- 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这四者的区别 ...
- [Oacle][Partition]Partition操作与 Index, Global Index 的关系
[Oacle][Partition]Partition操作与 Index, Global Index 的关系: ■ Regarding the local index and the global i ...
- Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...
- 转: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 ...
- thinkphp报错Call to undefined method app\index\controller\Index::fetch()
因为要写一个系统,所以又重新下载了thinkphp,然后安装了一下.回忆起这个问题很容易让新手朋友费解.会出现如下报错:Call to undefined method app\index\contr ...
随机推荐
- C#.NET 各种连接字符串
C#.NET 各种连接字符串 近期连接数据库时,经常忘记连接字符串是如何的格式,现在此备注 此文章引用http://www.cnblogs.com/zhiqiang-imagine/archive/2 ...
- shell参数
shell获取当前执行脚本的路径 filepath=$(cd "$(dirname "$0")"; pwd) 脚本文件的绝对路径存在了环境变量filepath中 ...
- 0115——cocoapod的使用
iOS 最新版 CocoaPods 的安装流程 1.移除现有Ruby默认源 $gem sources --remove https://rubygems.org/ 2.使用新的源 $gem sourc ...
- mysql设置字体
如果在linux下重启mysql服务的时候出现Job failed to start,在window下重启失败,这是因为你安装了高版本的mysql(mysql5.5以上),在高版本对字符编码方式修改的 ...
- poj2774 Long Long Message(后缀数组or后缀自动机)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Long Long Message Time Limit: 4000MS Me ...
- C# Struct结构体
C#中结构类型和类类型在语法上非常相似,他们都是一种数据结构,都可以包括数据成员和方法成员. 结构和类的区别: 1.结构是值类型,它在栈中分配空间:而类是引用类型,它在堆中分配空间,栈中保存的只是引用 ...
- 你真的会玩SQL吗?Top和Apply
原文:你真的会玩SQL吗?Top和Apply 本章预先想写一些Top和Apply基本的用法,但好像没什么意义,所以删掉了一些无用的东西,只留下几个示例,以保证系列的完整性. Top和Apply解决的常 ...
- Mysql.Data的连接驱动 .net 的源码竟然在git了
如标题 上链接:https://github.com/mysql/mysql-connector-net
- Linux下,连接器ld链接顺序的总结
原来ld对于链接一系列的库的顺序是很敏感的,不然会报undefined referenced 的函数符号错误,意思就是未找到函数定义.实际上库是能正确打开的.如果库libA.a依赖于库libB.a,那 ...
- Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏
Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...