日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

LeetCode 1966. Binary Searchable Numbers in an Unsorted Array

發布時間:2024/7/5 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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阿明),一起加油、一起學習進步!

總結

以上是生活随笔為你收集整理的LeetCode 1966. Binary Searchable Numbers in an Unsorted Array的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。