275. H-Index II
题目:
Follow up for H-Index: What if the citations
array is sorted in ascending order? Could you optimize your algorithm?
Hint:
- Expected runtime complexity is in O(log n) and the input is sorted.
链接: http://leetcode.com/problems/h-index-ii/
题解:
假如给定数组是排序好的,求H-Index。 我们就可以用Binary Search。 target = len - mid。
Time Complexity - O(logn), Space Complexity - O(1)
public class Solution {
public int hIndex(int[] citations) {
if(citations == null || citations.length == 0) {
return 0;
}
int len = citations.length, lo = 0, hi = len - 1; while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
if(citations[mid] == len - mid) {
return citations[mid];
} else if(citations[mid] > (len - mid)) {
hi = mid - 1;
} else {
lo = mid + 1;
}
} return len - lo;
}
}
题外话: 房子的装修师傅今天离开了,但是关键的电炉灶没接好,原因是总闸处220v电线有问题,导致插座没电。不过都是熟人,我也不想他们碰处一些危险的东西。虽然还要另找电工检查线路和接线,很费事费钱,但我想明年争取换份好工作,挣回来就好了。
二刷:
对于在位置i的论文来说, 至少len - i的论文, citations数目都比位置i的论文大。所以i位置的h-index至少是len - i。我们就可以用Binary Search来搜索答案。
Java:
public class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
int len = citations.length, lo = 0, hi = len - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (citations[mid] == len - mid) {
return citations[mid];
} else if (citations[mid] < len - mid) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return len - lo;
}
}
Reference:
https://leetcode.com/discuss/56122/standard-binary-search
https://leetcode.com/discuss/56170/java-binary-search-simple-and-clean
275. H-Index II的更多相关文章
- Leetcode之二分法专题-275. H指数 II(H-Index II)
Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...
- Java实现 LeetCode 275 H指数 II
275. H指数 II 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高 ...
- Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵
H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...
- 275 H-Index II H指数 II
这是 H指数 进阶问题:如果citations 是升序的会怎样?你可以优化你的算法吗? 详见:https://leetcode.com/problems/h-index-ii/description/ ...
- [LeetCode] 275. H-Index II H指数 II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- 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 ...
- [Swift]LeetCode275. H指数 II | H-Index II
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a ...
- Lintcode: Permutation Index II
Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- HDU-6278-Jsut$h$-index(主席树)
链接: https://vjudge.net/problem/HDU-6278 题意: The h-index of an author is the largest h where he has a ...
随机推荐
- To add private variable to this Javascript literal object
You can use number as function/variable name, the numberic name can't be accessed from parent scope, ...
- 目前国内外主流的linux发行版本
1.linux其实是基于unix发展而来的,还有mac os也是类unix操作系统 2.目前主流的linux发行版本主要有:红帽系列(中国大陆,美洲地区,发源于美国),suse系列(欧洲地区流行,发源 ...
- Web前端技能
入门必备的技能: 第1项技能:HTML超文本标记语言: 技能要点: HTML文件的结构 HTML文件的编写方法 HTML基本标记 文字与段落标记 框架 使用表单 ...
- LNMP系列网站零基础开发记录(一)
[目录] 扯淡吹逼之开发前奏 Django 开发环境搭建及配置 web 页面开发 Django app开发 Django 站点管理 Python 简易爬虫开发 Nginx&uWSGI 服务器配 ...
- 【Binary Tree Zigzag Level Order Traversal】cpp
题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...
- Codeforces Round #352 (Div. 2) C. Recycling Bottles 暴力+贪心
题目链接: http://codeforces.com/contest/672/problem/C 题意: 公园里有两个人一个垃圾桶和n个瓶子,现在这两个人需要把所有的瓶子扔进垃圾桶,给出人,垃圾桶, ...
- 2016ACM-ICPC Qingdao Online青岛网络赛题解
TonyFang+Sps+我=5/12 滚了个大粗 2016年9月21日16:42:36 10题完工辣 01 题意:求形同的数中大于n的最小值 题解:预处理所有的(5194个),在这里面二分 #inc ...
- A*(A星)算法Go lang实现
之前发表一个A*的python实现,连接:点击打开链接 最近正在学习Go语言,基本的语法等东西已经掌握了.但是纸上得来终觉浅,绝知此事要躬行嘛.必要的练手是一定要做的.正好离写python版的A*不那 ...
- aspx文件、aspx.cs文件、aspx.designer.cs文件之讲解
.aspx文件:(页面)书写页面代码.存储的是页面design代码.只是放各个控件的代码,处理代码一般放在.cs文件中. .aspx.cs文件:(代码隐藏页)书写类代码.存储的是程序代码.一般存放与数 ...
- AngularJs学习笔记--concepts(概念)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...