[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 ...
随机推荐
- oracle 创建用户并赋权 清空用户表
create user BUSM identified by BUSM; grant connect,resource,dba to BUSM; grant select any table to B ...
- 使用 Composer 安装 Laravel 框架
前言: 1. Composer 安装 Laravel 有两种方式: 第一种是通过 Composer 的 create-project 命令安装 Laravel 框架, 第二种是先通过 Composer ...
- apache配置多个站点
序:这次项目主要是为了给微信客户端添加一个地址,在微信公众号里面添加一个可以访问的app下载页面,说起来很简单,但总不能为了这么小的一个网站新建一个web服务器吧! 现在开始配置,首先必须确认已经在L ...
- poj3160强连通分量加dfs
After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such ...
- Linux下Tomcat进行远程调试
1.更改tomcat远程调试端口(可以使用默认端口不更改) 打开目录下的catalina.sh文件,找到JPDA_ADDRESS=”8000”,8000代表远程调试端口,可以更改成其他没有被占用的端口 ...
- ST-4
1.(49-7)使用下面的方法printPrimes()完成后面的问题: (a)为printPrimes()方法画控制流图. (b)考虑测试用例t1=(n=3)和t2=(n=5).即使这些测试用例游历 ...
- JS中作用域
var scope = 'global'; var f = function() { console.log(scope); // 输出 undefined var scope = 'f'; } f( ...
- 查看当前用户名称:whoami命令
没什么可讲的,就是显示当前用户名称,效果同"id -un"命令.
- tail命令
tail命令用来取文件后几行,默认显示后10行.有多个FILE,每个都带有一个头文件名称. 语法: tail [OPTION]... [FILE]... 选项: -n#:取文件后#行,n可省略: -c ...
- 【JAVAWEB学习笔记】19_事务
事务 学习目标 案例-完成转账 一.事务概述 1.什么是事务 一件事情有n个组成单元 要不这n个组成单元同时成功 要不n个单元就同时失败 就是将n个组成单元放到一个事务中 2.mysql的事务 默认的 ...