[Leetcode] Binary search--275 H-Index
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
Solution:
here we use binary search
inspired from sorting method c[i] < i+1;
when citations c is in ascending order, the idea is find the minimum index c[i] >= len(c) -i
l = 0
h = len(citations)-1
while (l <= h):
mid = (l+h)/2
if citations[mid] == len(citations)-mid:
return len(citations)-mid
elif citations[mid] < len(citations)-mid:
l = mid+1
else:
h = mid-1 return len(citations)-l
[Leetcode] Binary search--275 H-Index的更多相关文章
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
- LeetCode & Binary Search 解题模版
LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LeetCode——Binary Search Tree Iterator
Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...
随机推荐
- commitProperties方法
自定义的组件,如果重写commitProperties方法,那么在该方法内部一定要注意super.commitProperties()的调用.
- Centos6.5 rpm方式指定目录安装JDK
第一步:下载jdk本例使用jdk-7u5-linux-i586.rpm包,登陆到http://www.java.com下载即可. 第二步:检查文件权限及安装情况1.以root权限登陆系统,查看并修改使 ...
- CSS的position/float/display
一.position position属性取值:static(默认).relative.absolute.fixed.inherit. postision:static:始终处于文档流给予的位置.它可 ...
- 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home
https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...
- Laravel5中Cookie的使用
今天在Laravel框架中使用Cookie的时候,碰到了点问题,自己被迷糊折腾了半多小时.期间研究了Cookie的实现类,也在网站找了许多的资料,包括问答.发现并没有解决问题.网上的答案都是互相抄袭, ...
- C#使用NOPI导入Excel
使用NOPI导入Excel文档 NOPI版本:2.3.0,依赖于NPOI的SharpZipLib版本:0.86,经测试适用于.net4.0+ 记录遇到的几个问题 NOPI中的IWorkbook接口:x ...
- MongoDB3.4 shell CRUD操作
输入db,显示你正在操作的数据库:切换数据库,输入use dbName,如果数据库不存在的话会自动帮我们创建一个:使用show dbs可以显示所有可用的数据库. 测试数据在文末 插入文档 插入操作的行 ...
- java.util.NoSuchElementException: None.get的解决方法
在Java中用null表示无值返回.在Java 里,null 是一个关键字,不是一个对象,所以对它调用任何方法都是非法的 笔者就常在类型转换时见到空指针错误,便是由null的特殊性导致的 而在scal ...
- Scrapy 爬虫框架入门案例详解
欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:崔庆才 Scrapy入门 本篇会通过介绍一个简单的项目,走一遍Scrapy抓取流程,通过这个过程,可以对 ...
- Python进阶之装饰器
函数也是对象 要理解Python装饰器,首先要明白在Python中,函数也是一种对象,因此可以把定义函数时的函数名看作是函数对象的一个引用.既然是引用,因此可以将函数赋值给一个变量,也可以把函数作为一 ...