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
leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)的更多相关文章
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- 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 ...
- Leetcode之二分法专题-704. 二分查找(Binary Search)
Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 t ...
- [LeetCode]题解(python):098 Validate Binary Search Tree
题目来源 https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it ...
- 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- 154. Find Minimum in Rotated Sorted Array II(Binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...
- C#LeetCode刷题之#704-二分查找(Binary Search)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3999 访问. 给定一个 n 个元素有序的(升序)整型数组 num ...
随机推荐
- jvisualvm 使用
和jconsole侧重于内存分析和检测不同,jvisualvm在线程分析方面更强大一些,下面简单介绍下使用: 1. 在要监控的java应用配置文件中,本例是apache-jmeter/bin/jmet ...
- [codility]Min-abs-sum
https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉, ...
- 通过web代理进行跨域访问,http请求返回超时的问题定位
[现象] 在ajax通过web代理跨域访问时,http第一次登陆时正常,但是第二次再下发其他命令的时候总是返回 java.net.SocketTimeoutException: Read timed ...
- C# Winform应用程序占用内存较大解决方法整理
微软的 .NET FRAMEWORK 现在可谓如火如荼了.但是,.NET 一直所为人诟病的就是“胃口太大”,狂吃内存,虽然微软声称 GC 的功能和智能化都很高,但是内存的回收问题,一直存在困扰,尤其 ...
- 关于rewriteRule的一个小问题
RewriteEngine on # RewriteRule ^test.php$ modrewrite.php# RewriteRule ^(.*) http://www.baidu.com [L] ...
- Android开发之ListView-ArrayAdapter的使用
ArrayAdapter: ArrayAdapter<String>(Context context, int resource, int textViewResourceId, Stri ...
- 【转】The final local variable xxx cannot be assigned, since it is defined in an enclosing type
文地址:http://blog.163.com/benben_long/blog/static/199458243201481102257544/ 本文就自己编程时候遇到的一个问题,简要描述一下,并提 ...
- java参数传递时到底是值传递还是引用传递
java参数传递时到底是值传递还是引用传递(baidu搜集) 问”,很多人的BLOG里都引用这些面试题,最近因为工作内容比较枯燥,也来看看这些试题以调节一下口味,其中有一道题让我很费解. 原题是:当一 ...
- RazorEngine(未解决,留底)
TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { BaseTemplateType = ...
- [2015编程之美] 第一场A
#1156 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一 ...