前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1966. Binary Searchable Numbers in an Unsorted Array

LeetCode 1966. Binary Searchable Numbers in an Unsorted Array

作者头像
Michael阿明
发布2021-09-06 11:41:25
4440
发布2021-09-06 11:41:25
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

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:

代码语言:javascript
复制
func(sequence, target)
  while sequence is not empty
    randomly choose an element from sequence as the pivot
    if pivot = target, return true
    else if pivot < target, remove pivot and all elements to its left from the sequence
    else, remove pivot and all elements to its right from the sequence
  end while
  return 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.

代码语言:javascript
复制
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. 解题

  • 如果一个数,比他所有左边的数都大,比他所有右边的数都小,那么肯定能被二分查找找到
代码语言:javascript
复制
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阿明),一起加油、一起学习进步!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档