LeetCode #274. H-Index 数组
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."
Example:
Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] 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, her h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
思路
解法一
纯暴力解题。由题目得 h <= N,所以用双层循环,外层遍历 h 的所有可能取值,内层遍历 citations 求出 ≥ h 和 ≤ h 的个数。
用两个计数器分别统计 at least h 和 no more than h 的数字的出现个数,并用 if + continue 保证每个数字只被统计一次。注意,计数器 cnt1 统计了 h 个数字后就不再统计,以确保计算器 cnt2 能正确统计剩下 N - h 篇引用不超过 h 的论文。
由于计算器 cnt1 统计 h 个数字后就不再统计,我们就需要预先进行降序排序以避免计数错误,比如 [2, 1] 这组数据的计数结果应该与 [1, 2] 这组数据是相同的。
时间复杂度:O(n^2) = 排序 O(nlgn) + 两层循环 O(n^2)
空间复杂度:O(1)
耗时 185 ms, Memory 6.5 MB, ranking 5.56%
class Solution {
public:
int hIndex(vector<int> &citations) {
sort(citations.begin(), citations.end(), greater<int>());
int paper_num = citations.size();
int max_h_idx = 0; // h index for a scientist must include 0
for (int h = 1; h <= paper_num; ++h) {
int cnt1 = 0;
int cnt2 = 0;
for (int cit : citations) {
if (cnt1 != h && cit >= h) {
++cnt1;
continue; // guarantee only count every element once
}
if (cit <= h) {
++cnt2;
}
}
if (cnt1 == h && cnt2 == (paper_num - h)) {
max_h_idx = h;
}
}
return max_h_idx;
}
};
解法二
Wikipedia 给出了统计 H-Index 的算法:
- 将其发表的所有SCI论文按被引次数从高到低排序;
- 从前往后查找排序后的列表,直到某篇论文的序号大于该论文被引次数。所得序号减一即为H指数。
我理解的原理是:在这个算法中,h 值等于数组的索引值,在降序排序后,每次条件遍历一个元素,就说明至少有一篇论文的引用数 ≥ h,而数组剩下的另一半元素恰好就是那些引用数 ≤ h 的论文。因此,当索引值 i >= citations[i] 时,说明 citations[0..i] 这些论文的引用数是 ≥ h,而 citations[i..n] 这些论文的引用数是 ≤ h,刚好就是这道算法题的答案。
时间复杂度:O(n) = 排序 O(nlgn) + 遍历citations O(n)
空间复杂度:O(1)
耗时 4 ms, Memory 6.6 MB, ranking 70.58%
class Solution {
public:
int hIndex(vector<int> &citations) {
sort(citations.begin(), citations.end(), greater<int>());
for (int i = 0; i < citations.size(); ++i) {
if (i >= citations[i]) return i;
}
return citations.size();
}
};
参考
- 《[LeetCode] H-Index 求H指数》:https://www.cnblogs.com/grandyang/p/4781203.html
原文地址:https://www.cnblogs.com/Bw98blogs/p/12719937.html
- 【腾讯云的1001种玩法】试用腾讯云 Windows Server 2012 R2 镜像的几点经验分享
- 一个只有99行代码的JS流程框架(二)
- 看书的时候如何调试书中简单的C+代码?
- gcForest 集成学习方法的 Python 实现
- 云端架构师养成系列之一:高性能云硬盘入门与实战(视频)
- 云端架构师养成系列之二:云端负载均衡上手与实践
- 微信 PaxosStore:海量数据冷热分级架构
- 使用腾讯云容器服务来构建简单web service
- 使用 plotly 绘制数据图表
- 基于云计算的 CV 移动交互应用研究:头部姿态估计综述(2)
- 使用 trie 树实现简单的中文分词
- 重磅发布!2017年度 DevOps 现状调查报告中文完整版!
- AI 泡沫前,我们怎么办?中美两国人工智能产业发展全面解读
- 养车记账本小程序开发实例
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- PHP抽象类基本用法示例
- keras分类模型中的输入数据与标签的维度实例
- keras的三种模型实现与区别说明
- PHP智能识别收货地址信息实例
- Keras 在fit_generator训练方式中加入图像random_crop操作
- PHP面向对象程序设计继承用法简单示例
- php根据命令行参数生成配置文件详解
- PHP使用SOAP调用API操作示例
- 使用Zookeeper分布式部署PHP应用程序
- pytorch判断是否cuda 判断变量类型方式
- Keras搭建自编码器操作
- python程序如何进行保存
- Android Q之气泡弹窗的实现示例
- Python with语句用法原理详解
- pytorch 计算ConvTranspose1d输出特征大小方式