【刷题】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. 题 ...
随机推荐
- git HEAD游离状态问题解决
最近在迭代一个版本的时候,出现 HEAD detached at xxx 提示,应该是我切换分支的时候,哪里没弄对. 那么可以通过如下办法解决 git checkout 05 # 先checkou ...
- UAC 注册表 WIN64 OS 运行时主题
首先EXE程序是32位,DelphiIDE对Project默认是启用主题的,默认情况在WIN64 OS下运行时,无管理员权限. WIN64 OS,默认情况下UAC是启用的. 上述默认情况下,EXE 是 ...
- 编译EXE文件的时候pcap编译不进去。 pyinstaller pcap pypcap 报错
如果生成的exe源码中有import pcap 那么你目标机上就要先装npcap 并勾选winpcap API. 然后就不出这个问题了. 暂时的办法是第一个exe不包含import pcap.自检np ...
- 这是一个专注于电脑技术、软件应用、互联网、嵌入式,电子技术行业等的原创IT博客
http://www.choovin.com/ 这是一个专注于电脑技术.软件应用.互联网.嵌入式,电子技术行业等的原创IT博客
- OC基础:Date 分类: ios学习 OC 2015-06-22 19:16 158人阅读 评论(0) 收藏
NSDate 日期类,继承自NSObject,代表一个时间点 NSDate *date=[NSDate date]; NSLog(@"%@",date); //格林尼治时间, ...
- MySQL性能优化方法一:缓存参数优化
原文链接:http://isky000.com/database/mysql-perfornamce-tuning-cache-parameter 数据库属于 IO 密集型的应用程序,其主要职责就是数 ...
- matlab fgetl()
% % file=dir('/home/wang/Desktop/trainset/others/'); % % :length(file) % % path= strcat('/home/wang/ ...
- chapter02 回归模型在''美国波士顿房价预测''问题中实践
#coding=utf8 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_boston # 从sklearn.mo ...
- Adobe Flash Player - imsoft.cnblogs
Adobe Flash Player是一个跨平台.基于浏览器的应用程序.运行时,它可以跨屏幕和浏览器原汁原味地查看具有表现力的应用程序.内容和视频.Flash Player实现了移动屏幕上的高性能优化 ...
- js 如何控制文本域输入内容在一定间隔时间段才触发事件查询相关数据
<script>var flag = 0;var t;function openFlag () { t = setTimeout(function(){flag = 1; dosometh ...