当前位置:网站首页>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
边栏推荐
- Binary tree related code questions [more complete] C language
- 使用百度EasyDL实现森林火灾预警识别
- CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
- 我的 archinstall 使用手册
- 洛谷P4847 银河英雄传说V2
- 【FPGA】名词缩写
- Common layout effect realization scheme
- 这些云自动化测试工具值得拥有
- App Basic Framework Construction丨Log Management - KLog
- 洛谷P4560 Wall 砖墙
猜你喜欢
Description of ESB product development steps under cloud platform
这些云自动化测试工具值得拥有
WPF DataGrid 使用数据模板(2)
[Likou] 22. Bracket generation
【FPGA】day21- moving average filter
How to learn machine learning?machine learning process
es-head插件插入查询以及条件查询(五)
"104 Maximum Depth of Binary Trees" in LeetCode's Day 12 Binary Tree Series
机器学习中什么是集成学习?
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
随机推荐
What are port 80 and port 443?What's the difference?
如何给网页添加icon图标?
[C Language] Getting Started
shell monitors gpu usage
rub the heat - do not open
拼多多店铺营业执照相关问题
优先级队列
【服务器安装Redis】Centos7离线安装redis
Differences and connections between distributed and clustered
洛谷P2150 寿司晚宴
Mysql中事件和定时任务
LeetCode刷题第17天之《3 无重复字符的最长子串》
【FPGA】SDRAM
洛谷P4847 银河英雄传说V2
增加PRODUCT_BOOT_JARS及类 提供jar包给应用
【FPGA】day20-I2C读写EEPROM
WPF DataGrid 使用数据模板(2)
LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
Power Cabinet Data Monitoring RTU
"125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions