原题戳我

题目

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 return 2147483647.

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

分析

这道题我是看题解才做出来的,思路非常好,有两个重点:

  1. 可以O(logk)的时间缩小二分法的范围
  2. 从而,可以将二分的最坏时间优化到O(logk)

第二点无需证明,下面讲解第一点。

以O(logk)的时间缩小二分法的范围

如果从0遍历到k,那么明显时间复杂度为O(k),超过了了O(logk)。

要记得,我们的目的是确定一个数组的上界r,使O(r)=O(k),继而在这段数组上进行二分查找,复杂度为O(logk)。因此,我们只需要将在O(logk)的时间内找到该r。

r的要求如下:

  1. 满足O(r)=O(k)
  2. 计算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的更多相关文章

  1. LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  2. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. 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 ...

  4. 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 ...

  5. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  6. 算法题丨Remove Duplicates from Sorted Array

    描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...

  7. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  9. 【leetcode刷题笔记】Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

随机推荐

  1. 反向-代理 nginx for Mac 的配置以及使用

       反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端 ...

  2. bzoj1648

    题解: 简单灌水 然后统计一下 代码: #include<bits/stdc++.h> using namespace std; ; int ne[N],num,fi[N],n,k,m,x ...

  3. 51nod算法马拉松28-a

    题解:水体一枚 按照贪心的思想求出是2的k次方,然后高精度计算 代码: #include<bits/stdc++.h> using namespace std; ; int ans,n,a ...

  4. SharePoint 2013的100个新功能之内容管理(四)

    一:脚本编辑器Web部件 新的脚本编辑器Web部件表现为插入标签页下的Ribbon中的"嵌入的代码",可以使用户在SharePoint网站页面中添加HTML或Javascript或 ...

  5. C++中特殊的宏定义

    常规用法不再介绍,做如下几点说明和介绍 1. 带参数的宏只完成简单字符替换,之前不做计算实参的工作,如下 #define SUM(x,y) x+yint a=3,b=2,c=1;int s;s=SUM ...

  6. magento常见的问题及解决方法

    刚接触magento时,会遇到很多问题,大多数都是些magento配置及操作上的问题,因为刚接触magento不久所有对这些问题比较陌生也不知道如何处理.今日根据模版堂技术指导下和网上的相关例子,这里 ...

  7. opencv-python教程学习系列1-安装库

    前言 以后的项目可能会用到python和opencv进行实现,故准备opencv-python教程学习系列记录学习过程的点滴,这是这一系列的开篇,坚持学习,共同进步. 系列教程参照OpenCV-Pyt ...

  8. CentOS LAMP环境 配置详解

    要想在linux上实现网页服务器(www)需要Apache这个服务器软件,不过Apache仅能提供最基本的静态网站数据而已,想要实现动态网站的话,最好还是要PHP与MySQL的支持,所以下面我们将会以 ...

  9. Unity插件-NGUI使用教程

    Unity插件-NGUI使用教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 NGUI 一款强大的次 ...

  10. C#对文件I/O的一些基本操作

    System.IO命名空间包含允许在数据流和文件上进行同步,异步及写入的类型,下面是关于c#文件的I/O基本操作讲解,需要的朋友可以参考下 文件是一些永久存储及具有特定顺序的字节组成的一个有序的,具有 ...