原题戳我

题目

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. 模拟QQ分组

    package com.lixu.fenzu; import java.util.ArrayList; import java.util.HashMap; import android.app.Lis ...

  2. (C/C++学习笔记) 十六. 预处理

    十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...

  3. DevExpress v17.2新版亮点—WinForms篇(四)

    用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.开篇介绍了DevExpress WinForms v17.2 Data Grid Control ...

  4. 【数据库】MFC ODBC(二)

    三.CRecordset类 1.IsBOF与IsEOF (1)IsBOF 如果记录集没有记录,或已经向前游动到第一个记录之前,则返回非零:否则返回0.详细说明如下: 1)访问Open函数之后,如果记录 ...

  5. centos7 mysql的安装

    本文记录centos7安装mysql的一些过程与遇到的一些坑 下载mysql的压缩包,直接从官网上面下载,链接:http://dev.mysql.com/downloads/mysql/ 选择 MyS ...

  6. Vue.js 源码学习笔记 -- 分析前准备1 -- vue三大利器

    主体 实例方法归类:   先看个作者推荐, 清晰易懂的  23232 简易编译器   重点: 最简单的订阅者模式 // Observer class Observer { constructor (d ...

  7. 解决tensorflow的"Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA Using TensorFlow backend."警告问题

    问题描述 程序开始运行的时候报出警告:I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructio ...

  8. C/C++ 字符串分割: strtok 与 strsep 函数说明

    函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok ...

  9. Generate And Play A Tone In Android hacking

    /*********************************************************************************** * Generate And ...

  10. 广播中receiver配置需要注意data的配置

    1.sd卡的转载和卸载 这个需要配置好android:scheme=“file” 要不然是检测不到的 2.在安装应用或者卸载应用都要有一个约束,要不然是不会执行的.而这个约束条件为package. 但 ...