leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)
https://leetcode.com/problems/h-index/
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 otherN − 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.
Hint:
- An easy approach is to sort the array first.
- What are the possible values of h-index?
- A faster approach is to use extra space.
class Solution {
public:
int getIdx(vector<int>& vec, int val) {
if(vec.size() <= ) return -;
int low = , high = vec.size()-;
while(low < high) {
int mid = low + (high-low)/;
if(vec[mid] < val) low = mid+;
else high = mid;
}
return low;
}
int hIndex(vector<int>& citations) {
if(citations.size() == ) return ;
if(citations.size() == ) {
if(citations[] == ) return ;
return ;
}
sort(citations.begin(), citations.end());
if(citations[citations.size()-] == ) return ; int res = INT_MIN;
int low = , high = citations.size();
while(low < high) {
int mid = low + (high - low)/;
if(mid <= citations.size()-getIdx(citations, mid)) {
low = mid;
res = max(res, mid);
}
else high = mid;
if(high - low <= ) break;
} if(low <= citations.size()-getIdx(citations, low)) {
res = max(res, low);
}
if(high <= citations.size()-getIdx(citations, high)) {
res = max(res, high);
}
return res;
}
};
leetcode 263: H-Index
class Solution {
public:
int getIdx(vector<int>& vec, int val) {
if(vec.size() <= ) return -;
int low = , high = vec.size()-;
while(low < high) {
int mid = low + (high-low)/;
if(vec[mid] < val) low = mid+;
else high = mid;
}
return low;
}
int hIndex(vector<int>& citations) {
if(citations.size() == ) return ;
if(citations.size() == ) {
if(citations[] == ) return ;
return ;
}
//sort(citations.begin(), citations.end());
if(citations[citations.size()-] == ) return ; int res = INT_MIN;
int low = , high = citations.size();
while(low < high) {
int mid = low + (high - low)/;
if(mid <= citations.size()-getIdx(citations, mid)) {
low = mid;
res = max(res, mid);
}
else high = mid;
if(high - low <= ) break;
} if(low <= citations.size()-getIdx(citations, low)) {
res = max(res, low);
}
if(high <= citations.size()-getIdx(citations, high)) {
res = max(res, high);
}
return res;
}
};
leetcode 264: H-Index II
最新文章
- jQuery EasyUI Combobox 无法获取属性 options 的值: 对象为 null 或未定义
- 安装运行mariadb时错误:gtid_slave_pos
- mongdb查询与排序
- UI控件之ListView
- SignalR记录
- 菜鸟学四轴控制器之3:数字积分法DDA实现直线插补
- C#中如何查找Dictionary中的重复值
- mslookup
- My implementation of AVL tree
- [HDU 1695] GCD
- Oracle Enterprise Manager Cloud Control 12c R4 安装配置
- Linux上使用shell脚本查看内存情况(超实用)
- Java图书管理系统(用Java常用集合实现)
- Java设计模式之模板方法设计模式(银行计息案例)
- react_app 项目开发_遇到的坑
- 理解Shadow DOM(一)
- 在 Linux 中自动启动 Confluence 6
- 应用程序池--IIS最大工作进程数
- fcitx4.2.0自定义中文标点符号
- oracle建表,设置主键,修改属性等