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 ...
随机推荐
- 1.iOS第一个简单APP
大纲: iOS系统发展 UI和OC 简单的APP程序 程序的生命周期 1.iOS的系统发展 从1983年OC程序开始发展到2015年,30多年的时间,但这依然不是一个十分完善的语言,可以说现在都没 ...
- iOS系统相册的有关操作
iOS中,我们选择相册中的资源和调用摄像头可以使用 :UIImagePickerController类来完成,不使用UI我们可以通过:ALAssetsLibrary类来使用相册资源. 一. ALAss ...
- DOM元素尺寸和位置(clientwidth ,scrollwidth , offsetwidth.......)
[1] slientWidth 和 sclientHeight slientWidth:获取的是可视宽度 slientHeight:获取的是可视高度 <html> <head> ...
- Spark学习计划
本文档综合现在市面上的各类spark书籍,概括spark技术核心,"要事第一"原则,只抓核心,才能领悟实质. spark核心分类: 1.环境配置相关(编译.搭建.配置.启动脚本) ...
- 面试题:Java静态/非静态方法重写
1.非静态方法重写 public class Test { public static void main(String[] args) throws Exception { Tree pine = ...
- CDZSC_2015寒假新人(1)——基础 g
Description Ignatius likes to write words in reverse way. Given a single line of text which is writt ...
- Windows I/O模型之一:Select模型
1.概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock) 四种调用模式: 同步:所谓同步,就是在发出一个功能调用时,在没有得到结果 ...
- tomcat7.0的源码下载
如果想知道servlet的HttpServlet的实现细节,想知道jsp的org.apache.jasper.runtime.HttpJspBase的实现细节,想知道tomcat关于servlet和j ...
- css派生选择器
后代选择器:即包含选择器,选择某元素的后代元素. 子元素选择器:只能选择某元素的子元素. 相邻兄弟选择器:可选择紧接在另一个元素后的元素,且两者有相同的夫元素.
- mac Word 怎样放大缩小文档结构图文字大小
在文档结构图的侧栏里按住control+option,然后滑动鼠标滚轮/双指上下滚动触摸板.