【刷题】Search in a Big Sorted Array
原题戳我。
题目
Description
Given a big sorted array with positive integers sorted by ascending order. The array is so big so that you can not get the length of the whole array directly, and you can only access the kth number by ArrayReader.get(k) (or ArrayReader->get(k) for C++). Find the first index of a target number. Your algorithm should be in O(log k), where k is the first index of the target number.
Return -1, if the number doesn't exist in the array.
Notice:
If you accessed an inaccessible index (outside of the array), ArrayReader.get will return2147483647.
Example
- Given [1, 3, 6, 9, 21, ...], and target = 3, return 1.
- Given [1, 3, 6, 9, 21, ...], and target = 4, return -1.
Challenge
O(log k), k is the first index of the given target number.
Tags
Sorted Array Binary Search
分析
这道题我是看题解才做出来的,思路非常好,有两个重点:
- 可以O(logk)的时间缩小二分法的范围
- 从而,可以将二分的最坏时间优化到O(logk)
第二点无需证明,下面讲解第一点。
以O(logk)的时间缩小二分法的范围
如果从0遍历到k,那么明显时间复杂度为O(k),超过了了O(logk)。
要记得,我们的目的是确定一个数组的上界r,使O(r)=O(k),继而在这段数组上进行二分查找,复杂度为O(logk)。因此,我们只需要将在O(logk)的时间内找到该r。
r的要求如下:
- 满足O(r)=O(k)
- 计算r的时间为O(logk)
即,寻找一个运算,进行O(logk)次,结果为O(k)。于是想到了乘幂:
2 ** O(logk) = O(k)
代码如下:
private int[] computeRange(ArrayReader reader, int target){
int r = 1;
while (reader.get(r) < target) {
r <<= 1;
}
int l = r >> 1;
while (r >= l && reader.get(r) == 2147483647) {
r--;
}
if (r < l) {
return null;
}
return new int[]{l, r};
}
完整代码
/**
* Definition of ArrayReader:
*
* class ArrayReader {
* // get the number at index, return -1 if index is less than zero.
* public int get(int index);
* }
*/
public class Solution {
/**
* @param reader: An instance of ArrayReader.
* @param target: An integer
* @return : An integer which is the index of the target number
*/
public int searchBigSortedArray(ArrayReader reader, int target) {
// write your code here
if (reader == null) {
return -1;
}
if (reader.get(0) > target) {
return -1;
}
int[] range = computeRange(reader, target);
if (range == null) {
return -1;
}
int k = bsearchLowerBound(reader, range[0], range[1], target);
if (reader.get(k) != target) {
return -1;
}
return k;
}
private int[] computeRange(ArrayReader reader, int target){
int r = 1;
while (reader.get(r) < target) {
r <<= 1;
}
int l = r >> 1;
while (r >= l && reader.get(r) == 2147483647) {
r--;
}
if (r < l) {
return null;
}
return new int[]{l, r};
}
private int bsearchLowerBound(ArrayReader reader, int l, int r, int v) {
while (l < r) {
int m = l + (r - l) / 2;
if (reader.get(m) >= v) {
r = m;
} else {
l = m + 1;
}
}
return l;
}
}
本文链接:【刷题】Search in a Big Sorted Array
作者:猴子007
出处:https://monkeysayhi.github.io
本文基于 知识共享署名-相同方式共享 4.0 国际许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名及链接。
【刷题】Search in a Big Sorted Array的更多相关文章
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- lintcode 447 Search in a Big Sorted Array
Given a big sorted array with positive integers sorted by ascending order. The array is so big so th ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 【leetcode刷题笔记】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
随机推荐
- 快速切题 poj1129 Channel Allocation
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12334 Accepted: 63 ...
- L1-011 A-B
本题要求你计算A−B.不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B. 输入格式: 输入在2行中先后给出字符串A和B.两字符串的长度 ...
- 使用MyEclipse开发Java EE应用:企业级应用程序项目(上)
你开学,我放价!MyEclipse线上狂欢继续!火热开启中>> [MyEclipse最新版下载] 一.EAR项目模型 MyEclipse提供企业应用程序项目模型,即EAR项目模型,以及用于 ...
- 关于plantera
在Plantera,您可以建立属于您自己的花园,并且看着新的植物,灌木,树木和动物一起生长. 当您进行游戏,扩张您的花园时,您会吸引圆滚滚的蓝色生物小助手们,它们将帮助您捡果子,收获您的植物 有时候会 ...
- Java实现责任链模式
责任链模式: 将接受者对象连成一条链,并在该链上传递请求,直到一个几首这对象处理它.通过让更多对象有机会处理请求,避免了请求发送者和接受者之间的耦合. 责任链模式的优缺点: 优点:高内聚,低耦合.业务 ...
- Wrapper class webservice.jaxws.SayHi is not found. Have you run APT to generate them?
最近在研究webservice,利用jdk来实现简单的webservice发布,但是运行时却发生了这样的异常,如下: Exception in thread "main" com. ...
- 在Emacs中使用GNU Global
背景 在我平时用Emacs编写C代码时,经常需要进行代码的跳转,主要需求为函数定义的跳转,某个具体函数的调用查找,某个结构体的定义跳转以及结构体中具体某一项的跳转等,GNU Global就能完全满足我 ...
- Python学习(004)-字典{}
特点: 无序状态 键唯一 不可变类型:字符串.整型.元组 可变类型:列表.字典 字典创建 第一种: dic1={','sex':'man'} print(dic1['name']) ----- ...
- HDU 4240
http://acm.hdu.edu.cn/showproblem.php?pid=4240 题意:求最大流和流量最大的一条路径的流量的比值 题解:流量最大的路径的流量在dinic的dfs每次搜到终点 ...
- Dockerfile构建mysql数据库
dockerfile文件: FROM centos:centos6 MAINTAINER admin "admin@topsec.com.cn" RUN yum insta ...