当前位置:网站首页>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
边栏推荐
- .NET Custom Middleware
- Mysql中事件和定时任务
- 关于pom.xml文件
- 解决多线程调用sql存储过程问题
- How to rebuild after pathman_config and pathman_config_params are deleted?
- set_new_handler(0)是什么意思?有什么用?
- LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
- 【Web3 系列开发教程——创建你的第一个 NFT(9)】如何在手机钱包里查看你的 NFT
- Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
- Common layout effect realization scheme
猜你喜欢
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
洛谷P2150 寿司晚宴
How to learn machine learning?machine learning process
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
【人话版】WEB3将至之“权益的游戏”
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
es-head plugin insert query and conditional query (5)
【yolov7系列三】实战从0构建训练自己的数据集
Binary tree related code questions [more complete] C language
随机推荐
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
Get Qt installation information: including installation directory and various macro addresses
【FPGA】day22-SPI协议回环
js 将字符串作为js执行代码使用
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
交换机--- 生成树--三层架构总结
"239 Sliding Window Maximum Value" on the 16th day of LeetCode brushing
蹭个热度-请勿打开
LeetCode刷题第10天字符串系列之《125回文串验证》
【FPGA】SDRAM
洛谷P4061 大吉大利,晚上吃鸡
What is machine learning?Explain machine learning concepts in detail
rub the heat - do not open
"104 Maximum Depth of Binary Trees" in LeetCode's Day 12 Binary Tree Series
[FPGA] day19- binary to decimal (BCD code)
Provincial level of Echart maps, as well as all prefecture-level download and use
What is third-party payment?
一文读懂 高性能可预期数据中心网络
typedef defines the structure array type