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

leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)的更多相关文章

  1. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  2. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  3. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

  4. [LeetCode]题解(python):098 Validate Binary Search Tree

    题目来源 https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it ...

  5. 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

    669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...

  7. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  8. 154. Find Minimum in Rotated Sorted Array II(Binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...

  9. C#LeetCode刷题之#704-二分查找(Binary Search)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3999 访问. 给定一个 n 个元素有序的(升序)整型数组 num ...

随机推荐

  1. SDUT 2527 斗地主

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2527 思路 :以前的结训比赛,当时不会做,比完 ...

  2. 移动wabAPP 开发 viewport 注意事项

    我们在开发移动设备的网站时,最常见的的一个动作就是把下面这个东西复制到我们的head标签中: <meta name="viewport" content="widt ...

  3. java使用redis

    1.redis和memecache这种缓存框架,都分为服务器端和客户端. 我们在项目中使用,相当于客户端,要引入客户端使用redis的jar包 首先你需要下载驱动包,下载 jedis.jar,确保下载 ...

  4. 【转】linux常用命令全集

    linux常用命令全集

  5. Android:实现无标题的两种方法

    实现无标题的两种方法:配置xml文件和编写代码设置 1.在AndroidManifest.xml文件中进行配置 实现全屏效果: android:theme="@android:style/T ...

  6. MAC OS Nginx php-fpm相关

    Nginx 命令 sudo nginx // 启动Nginx #重新加载|重启|停止|退出 sudo nginx -s reload|reopen|stop|quit #上传文件限制更改: 进入ngi ...

  7. 200. Number of Islands

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  8. P140、面试题24:二叉搜索树的后序遍历序列

    题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 测试用例: 1)功能测试(输入的后序遍历的序列 ...

  9. [译]Atomic VS. Non-Atomic 操作

    原文链接:atomic-vs-non-atomic-operations 在网上已经写了很多关于原子操作的文章,但是通常都集中在原子的读-修改-写(RMW. read-modify-write)操作. ...

  10. [转]vim ruby等的ide设置

    使用vim做rails开发,推荐这个 https://github.com/carlhuda/janus 1. vim下的Rails常用插件 首先列出我比较常用的vim插件,基本都是网上提到的哪些.必 ...