文章目录

  • 1. 题目
  • 2. 解题

1. 题目

Consider a function that implements an algorithm similar to Binary Search.
The function has two input parameters: sequence is a sequence of integers, and target is an integer value.
The purpose of the function is to find if the target exists in the sequence.

The pseudocode of the function is as follows:

func(sequence, target)while sequence is not emptyrandomly choose an element from sequence as the pivotif pivot = target, return trueelse if pivot < target, remove pivot and all elements to its left from the sequenceelse, remove pivot and all elements to its right from the sequenceend whilereturn false

When the sequence is sorted, the function works correctly for all values.
When the sequence is not sorted, the function does not work for all values, but may still work for some values.

Given an integer array nums, representing the sequence, that contains unique numbers and may or may not be sorted, return the number of values that are guaranteed to be found using the function, for every possible pivot selection.

Example 1:
Input: nums = [7]
Output: 1
Explanation:
Searching for value 7 is guaranteed to be found.
Since the sequence has only one element,
7 will be chosen as the pivot.
Because the pivot equals the target, the function will return true.Example 2:
Input: nums = [-1,5,2]
Output: 1
Explanation:
Searching for value -1 is guaranteed to be found.
If -1 was chosen as the pivot, the function would return true.
If 5 was chosen as the pivot, 5 and 2 would be removed.
In the next loop, the sequence would have only -1 and the function would return true.
If 2 was chosen as the pivot, 2 would be removed. In the next loop,
the sequence would have -1 and 5.
No matter which number was chosen as the next pivot,
the function would find -1 and return true.Searching for value 5 is NOT guaranteed to be found.
If 2 was chosen as the pivot, -1, 5 and 2 would be removed.
The sequence would be empty and the function would return false.Searching for value 2 is NOT guaranteed to be found.
If 5 was chosen as the pivot, 5 and 2 would be removed.
In the next loop, the sequence would have only -1 and the function would return false.Because only -1 is guaranteed to be found, you should return 1.Constraints:
1 <= nums.length <= 10^5
-10^5 <= nums[i] <= 10^5
All the values of nums are unique.

Follow-up: If nums has duplicates, would you modify your algorithm? If so, how?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-searchable-numbers-in-an-unsorted-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 如果一个数,比他所有左边的数都大,比他所有右边的数都小,那么肯定能被二分查找找到
class Solution {public:int binarySearchableNumbers(vector<int>& nums) {int n = nums.size(), ans = 0;vector<int> lmax(n, INT_MIN), rmin(n, INT_MAX);int MAX = INT_MIN, MIN = INT_MAX;for(int i = 0; i < n; ++i){MAX = max(MAX, nums[i]);lmax[i] = MAX;}for(int i = n-1; i >= 0; --i){MIN = min(MIN, nums[i]);rmin[i] = MIN;}bool a, b;for(int i = 0; i < n; ++i){a=b=false;if((i>0 && nums[i]>lmax[i-1]) || (i==0))a = true;if((i<n-1 && nums[i]<rmin[i+1]) || (i==n-1))b = true;if(a && b)ans++;}return ans;}
};

80 ms 48 MB C++


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

LeetCode 1966. Binary Searchable Numbers in an Unsorted Array相关推荐

  1. Leetcode 448. Find All Numbers Disappeared in an Array

    Leetcode  448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions ...

  2. LeetCode 448. Find All Numbers Disappeared in an Array 442. Find All Duplicates in an Array

    这两道题很有意思,由于元素为1~n,因此每个元素的值-1(映射到0~n-1)就可以直接当做下标.这样将 nums 中对应下标的元素 *-1 以i表示 index+1 这个元素出现过了,能节省存储的空间 ...

  3. leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)

    题目 https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题解 遍历数组,将数组中每个数字 n 作为下标,将 ...

  4. LeetCode之Find All Numbers Disappeared in an Array

    1.题目 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice a ...

  5. leetcode 448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  6. [swift] LeetCode 448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  7. LeetCode: 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode: 103. Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode: Flatten Binary Tree to Linked List

    LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...

最新文章

  1. count(1)、count(*) 与 count (列名) 的执行区别
  2. git stage 暂存_Git撤销暂存区stage中的内容
  3. Window对象的判定方法
  4. strace命令学习
  5. 在Win2003服务器系统中添加Web虚拟主机(图)
  6. gc的原因 频繁full_系统缓慢+CPU 100%+频繁Full GC问题的定位排查思路!
  7. 【机器学习】从Few-shot Learning再次认识机器学习
  8. 计算机信息处理教案,冀教版七年级信息技术第二课计算机--信息处理工具 教案...
  9. js理解 call( ) | apply( ) | caller( ) | callee( )
  10. delphi gui编辑工具源码_Python 快速构建一个简单的 GUI 应用
  11. kafka应用场景Kafka VS Flume
  12. 乐视网回击贾跃亭:债务处理没有进展,先拿出57亿再说
  13. 表情识别(六)--局部特征学习和Handcrafted特征结合
  14. 2018中国移动校招笔试记录
  15. 计算机自带仿真软件,crt软件(电脑终端仿真工具)V8.5.4 最新版
  16. 高项考试-信息化知识
  17. 数据原理——2、ChIA-PET
  18. pdf转成jpg或png的方法
  19. 2015年可视化研究前沿动态
  20. 非常好的产品研发管理文章,后面问题回答的很精彩(转)

热门文章

  1. Python中的函数概述
  2. 关于安装deepin+window10双系统有时没有声音的问题
  3. PHP Cookie和Session
  4. grpc php 返回值过大,使用grpc实现php、java、go三方互调
  5. 计算机一级繁体字转换,繁体字转换器
  6. 第二季1:图像基础知识
  7. 小程序购物车抛物线(贝塞尔曲线实现)
  8. [BZOJ 4025] 二分图
  9. jquery深入学习
  10. 【BZOJ-2435】道路修建 (树形DP?)DFS