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 ...
随机推荐
- 写给 iOS 开发者的 Hopper + lldb 简介
最近,关于 @Steipete 在Radar发布的帖子,笔者看到很多人在问「你是怎么理解那个伪代码的」.笔者想写博客已经有一段时间了,现在正好就此发表第一篇博文.笔者在一个叫 Hopper 的工具上花 ...
- EasyUI combotree值的设置 setValue
如果combotree的json数据如下: [ { "id":"2", "text":"wwwww", "st ...
- C++中的INL
inl文件介绍 inl文件是内联函数的源文件.内联函数通常在C++头文件中实现,但是当C++头文件中内联函数过多的情况下,我们想使头文件看起来简洁点,能不能像普通函数那样将内联函数声明和函数定义放在头 ...
- 在Oracle 11g r2中,EXP无法导出个别空的表
在Oracle 11g r2中,发现传统的exp无法不能导出空的表,上网搜索了一下找到了原因. 主要是Oracle 11g 新增了一个参数:deferred_segment_creation,含义是段 ...
- [原]捉虫记3:_ConectionPtr指针调用open失败
背景 产品使用MySQL来存储报警服务产生的报警.在报警服务的组件中使用ADO接口 客户方有两台计算机,一台计算机A用来组态,且可以对设备进行调试,操作系统是Win7 64bit 专业版,安装了VS2 ...
- jqueryrotate 使用 帮助 笔记 学习
1.想变角度 $("imgID").rotate(45); 2.想变角度时,有运动过程 $("imgID").rotate({animateTo:45} ...
- JavaScript DOM高级程序设计 5动态修改样式和层叠样式表1(源代码)--我要坚持到底!
W3C DOM2样式规范 现在这边贴出本章要的源代码,注意要结合前面用到的ADS库http://vdisk.weibo.com/s/Dq8NU CSSStyleSheet对象属性: type :始终是 ...
- brew,gem,rvm 和 bundler软件包的管理工具
brew是OS X上提供软件包的管理.Homebrew将软件包安装到单独的目录,然后符号链接到/usr/local 中,完全基于git和ruby.使用gem来安装你的gems,用brew来搞定他们的依 ...
- 解决windows下vim方向键变成 ABCD 的问题
一.问题描述: windows下面要安装git --> 安装了 msys2 -->安装并更新了 vim(7.4),然后在使用过程中发现vim不能使用 BACKSPACE 键,按方向键会打 ...
- 第一部分 android display(sufaceflinger & overlay)
最近在做0718的framebuffer驱动,fb驱动本身还是比较简单的,但重要的是需要按照android实现fb驱动的overlay特性,因此转一些关于android overlay的文章,以供以后 ...