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 3citations 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:

  1. An easy approach is to sort the array first.
  2. What are the possible values of h-index?
  3. 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

最新文章

  1. jQuery EasyUI Combobox 无法获取属性 options 的值: 对象为 null 或未定义
  2. 安装运行mariadb时错误:gtid_slave_pos
  3. mongdb查询与排序
  4. UI控件之ListView
  5. SignalR记录
  6. 菜鸟学四轴控制器之3:数字积分法DDA实现直线插补
  7. C#中如何查找Dictionary中的重复值
  8. mslookup
  9. My implementation of AVL tree
  10. [HDU 1695] GCD
  11. Oracle Enterprise Manager Cloud Control 12c R4 安装配置
  12. Linux上使用shell脚本查看内存情况(超实用)
  13. Java图书管理系统(用Java常用集合实现)
  14. Java设计模式之模板方法设计模式(银行计息案例)
  15. react_app 项目开发_遇到的坑
  16. 理解Shadow DOM(一)
  17. 在 Linux 中自动启动 Confluence 6
  18. 应用程序池--IIS最大工作进程数
  19. fcitx4.2.0自定义中文标点符号
  20. oracle建表,设置主键,修改属性等

热门文章

  1. Android:使用ViewPager实现左右滑动切换图片(图上有点点)
  2. 15个必知的Android开发者选项
  3. linux非阻塞的socket EAGAIN的错误处理
  4. 你想建设一个能承受500万PV/每天的网站吗?
  5. How to learn C++ and find all STL Algorithm reference
  6. C#中的线程(三) 使用多线程
  7. 【HDOJ】4347 The Closest M Points
  8. mac tree命令
  9. hdu2642Fliping game
  10. JAVA高级特性 - 注解