LeetCode 1966. Binary Searchable Numbers in an Unsorted Array

文章目录

    • 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阿明),一起加油、一起学习进步!
Michael阿明

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/471832.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

12 哈希表相关类——Live555源码阅读(一)基本组件类

12 哈希表相关类——Live555源码阅读(一)基本组件类 这是Live555源码阅读的第一部分&#xff0c;包括了时间类&#xff0c;延时队列类&#xff0c;处理程序描述类&#xff0c;哈希表类这四个大类。 本文由乌合之众 lym瞎编&#xff0c;欢迎转载 http://www.cnblogs.com/oloroso…

将方法作为方法的参数 —— 理解委托

《.NET开发之美》上对于委托写到&#xff1a;“它们就像是一道槛儿&#xff0c;过了这个槛的人&#xff0c;觉得真是太容易了&#xff0c;而没有过去的人每次见到委托和事件就觉得心里别得慌&#xff0c;混身不自在。”我觉得这句话就像是在说我自己一样。于是我决定好好看看关…

unix架构

UNIX Kernel&#xff08;UNIX内核&#xff09;&#xff1a;指挥机器的运行&#xff0c;控制计算机的资源 UNIX Shell(UNIX外壳&#xff09;&#xff1a;是UNIX内核和用户的接口&#xff0c;是UNXI的命令解释器。目前常用的Shell有3种Bourne Shell(B Shell): 命令sh。最老。Korn…

randn函数加噪声_语义分割中常用的损失函数1(基础篇)

一、L1、L2 loss (分割中不常用&#xff0c;主要用于回归问题)L1 LossL1 Loss 主要用来计算 input x 和 target y 的逐元素间差值的平均绝对值.pytorch表示为&#xff1a;torch.nn.functional.l1_loss(input, target, size_averageTrue)size_average主要是考虑到minibatch的情况…

LeetCode MySQL 1607. 没有卖出的卖家

文章目录1. 题目2. 解题1. 题目 表: Customer ------------------------ | Column Name | Type | ------------------------ | customer_id | int | | customer_name | varchar | ------------------------customer_id 是该表主键. 该表的每行包含网上商城的每一位…

LeetCode MySQL 1623. 三人国家代表队

文章目录1. 题目2. 解题1. 题目 表: SchoolA ------------------------ | Column Name | Type | ------------------------ | student_id | int | | student_name | varchar | ------------------------student_id 是表的主键 表中的每一行包含了学校A中每一个学…

LeetCode MySQL 1633. 各赛事的用户注册率

文章目录1. 题目2. 解题1. 题目 用户表&#xff1a; Users ---------------------- | Column Name | Type | ---------------------- | user_id | int | | user_name | varchar | ----------------------user_id 是该表的主键。 该表中的每行包括用户 ID 和用户…

LeetCode MySQL 1747. 应该被禁止的Leetflex账户

文章目录1. 题目2. 解题1. 题目 表: LogInfo ----------------------- | Column Name | Type | ----------------------- | account_id | int | | ip_address | int | | login | datetime | | logout | datetime | -----------------------该表是…

linux vim配置c,Linux入门学习教程:GNU C及将Vim打造成C/C++的半自动化IDE

C语言在Linux系统中的重要性自然是无与伦比、不可替代&#xff0c;所以我写Linux江湖系列不可能不提C语言。C语言是我的启蒙语言&#xff0c;感谢C语言带领我进入了程序世界。虽然现在不靠它吃饭&#xff0c;但是仍免不了经常和它打交道&#xff0c;特别是在Linux系统下。Linux…

LeetCode MySQL 1661. 每台机器的进程平均运行时间

文章目录1. 题目2. 解题1. 题目 表: Activity ------------------------- | Column Name | Type | ------------------------- | machine_id | int | | process_id | int | | activity_type | enum | | timestamp | float | --------------…

LeetCode MySQL 1741. 查找每个员工花费的总时间

文章目录1. 题目2. 解题1. 题目 表: Employees ------------------- | Column Name | Type | ------------------- | emp_id | int | | event_day | date | | in_time | int | | out_time | int | -------------------(emp_id, event_day, in_time) 是这个表…

LeetCode MySQL 1777. 每家商店的产品价格(行列转换)

文章目录1. 题目2. 解题1. 题目 表&#xff1a;Products ---------------------- | Column Name | Type | ---------------------- | product_id | int | | store | enum | | price | int | ----------------------(product_id,store) 是这个表的…

记事本linux命令换行符,Windows 10版记事本应用终于支持Linux/Mac换行符 排版不再辣眼睛...

记事本(Notepad)是微软 Windows 操作系统中相当经典的一款工具&#xff0c;其在最新的 Windows 10 操作系统中也得到了保留&#xff0c;命运比被 Photos 和 Paint 3D 取代的画图(MsPaint)程序要好得多。不过最近&#xff0c;Windows10 版记事本应用迎来了一项技能更新&#xff…

LeetCode 1885. Count Pairs in Two Arrays(二分查找)

文章目录1. 题目2. 解题1. 题目 Given two integer arrays nums1 and nums2 of length n, count the pairs of indices (i, j) such that i < j and nums1[i] nums1[j] > nums2[i] nums2[j]. Return the number of pairs satisfying the condition. Example 1: Inpu…

How to Avoid Producing Legacy Code at the Speed of Typing

英语不好翻译很烂。英语好的去看原文。 About the Author I am a software architect/developer/programmer.I have a rather pragmatic approach towards programming, but I have realized that it takes a lot of discipline to be agile. I try to practice good craftsman…

c语言程序做成可执行文件,windows环境下C程序生成可执行文件

windows环境下&#xff0c;编写C程序&#xff0c;生成.exe&#xff0c;用于操作某个文件。包含三部分&#xff1a;搭建环境、程序实现、程序分析。1、搭建程序编写和编译环境在windows下安装Git Bash(下载页面)。安装完成后&#xff0c;可以在windows的任意文件夹下&#xff0c…

LeetCode MySQL 1890. 2020年最后一次登录(year)

文章目录1. 题目2. 解题1. 题目 表: Logins -------------------------- | 列名 | 类型 | -------------------------- | user_id | int | | time_stamp | datetime | --------------------------(user_id, time_stamp) 是这个表的主键。 每一…

LeetCode MySQL 1873. 计算特殊奖金(case when then else end)

文章目录1. 题目2. 解题1. 题目 表: Employees ---------------------- | 列名 | 类型 | ---------------------- | employee_id | int | | name | varchar | | salary | int | ----------------------employee_id 是这个表的主键。 此表的每…

LeetCode 1868. 两个行程编码数组的积(双指针)

文章目录1. 题目2. 解题2.1 模拟超时2.2 优化1. 题目 行程编码&#xff08;Run-length encoding&#xff09;是一种压缩算法&#xff0c;能让一个含有许多段连续重复数字的整数类型数组 nums 以一个&#xff08;通常更小的&#xff09;二维数组 encoded 表示。 每个 encoded[…

LeetCode MySQL 1587. 银行账户概要 II

文章目录1. 题目2. 解题1. 题目 表: Users ----------------------- | Column Name | Type | ----------------------- | account | int | | name | varchar | -----------------------account 是该表的主键. 表中的每一行包含银行里中每一个用户的账号…