当前位置:网站首页>Snap - rotate the smallest number of an array
Snap - rotate the smallest number of an array
2022-08-11 04:21:00 【cbys-1357】
Title:
Move the first elements of an array to the end of the array, which we call the rotation of the array.
Given you an array numbers with possible duplicate element values, it turns out to be an array in ascending order, rotated once as above.Please return the smallest element of the rotated array.For example, the array [3,4,5,1,2] is one rotation of [1,2,3,4,5] and the minimum value of the array is 1.
Note that the result of one rotation of the array [a[0], a[1], a[2], ..., a[n-1]] is the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]] .
Example 1:
Enter:numbers = [3,4,5,1,2]Output:1
Example 2:
Input:numbers = [2,2,2,0,1]Output:0
Binary search
The title given in the question is a semi-ordered array. Although the traditional binary tells us that the binary can only be used in the ordered array, in fact, as long as it is a problem that can be reduced and cured, the binary can still be used.Thought.
Idea: The most special positions in the array are the left position left and the right position right, compare them with the value of the middle position mid, and then determine where the smallest number appears.
Is it possible to compare the values of the left position left and the middle position mid?
For example: [3, 4, 5, 1, 2] and [1, 2, 3, 4, 5], at this time, the value in the middle position is larger than the left, but the minimum value is one at the back and the other at thefront, so this approach cannot be effectively reduced.
Is it possible to compare the values of the right position right and the middle position mid?
Example: [1, 2, 3, 4, 5], [3, 4, 5, 1, 2], [2, 3, 4, 5 ,1], compare the elements at the right and middle positions, you can further narrow the scope of your search.
Additional note: When nums[mid] == nums[right], you can't rashly decide which side of the conclusion is the smallest number, but it is certain that discarding right will not affect the result.
Code implementation:
class Solution {public int minArray(int[] numbers) {int low=0;int height=numbers.length-1;while(lownumbers[height]){low=mid+1;}else if(numbers[mid]==numbers[height]){height--;}else{height=mid;}}return numbers[low];}} This question is to use the idea of binary search to continuously narrow the scope, and finally find the minimum value.
Event address: CSDN 21-day Learning Challenge
边栏推荐
猜你喜欢

Provincial level of Echart maps, as well as all prefecture-level download and use

LeetCode刷题第17天之《3 无重复字符的最长子串》

【FPGA】设计思路——I2C协议

对象的创建以及显示转换

使用jackson解析json数据详讲
![[Likou] 22. Bracket generation](/img/f6/435fe9e0b4c1545514d1bf195ffd44.png)
[Likou] 22. Bracket generation

"98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series

【FPGA】day21-移动平均滤波器

Build Zabbix Kubernetes cluster monitoring platform

leetcode刷题第13天二叉树系列之《98 BST及其验证》
随机推荐
移动端地图开发选择哪家?
增加PRODUCT_BOOT_JARS及类 提供jar包给应用
Pinduoduo store business license related issues
.NET 服务注册
The custom of the C language types -- -- -- -- -- - structure
es-head插件插入查询以及条件查询(五)
Use jackson to parse json data in detail
WPF DataGrid 使用数据模板(2)
【组成原理 九 CPU】
Will oracle cardinality affect query speed?
rub the heat - do not open
洛谷P4560 Wall 砖墙
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
set_new_handler(0)是什么意思?有什么用?
每日一题-滑动窗口
A simple JVM tuning, learn to write it on your resume
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
.NET service registration
一文读懂 高性能可预期数据中心网络
How to delete statements audit log?