当前位置:网站首页>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
边栏推荐
- How to learn machine learning?machine learning process
- 关于pom.xml文件
- The FTP error code list
- The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
- 【C语言】入门
- Leetcode 669. 修剪二叉搜索树
- Basic understanding of MongoDB (2)
- Audio codec, using FAAC to implement AAC encoding
- 快速使用UE4制作”大场景游戏“
- 【FPGA】day18-ds18b20实现温度采集
猜你喜欢
如何给网页添加icon图标?
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
Build Zabbix Kubernetes cluster monitoring platform
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
[C Language] Getting Started
2022-08-10 The sixth group Hiding spring study notes
WPF DataGrid 使用数据模板(2)
LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
LeetCode刷题第16天之《239滑动窗口最大值》
What is machine learning?Explain machine learning concepts in detail
随机推荐
移动端地图开发选择哪家?
Where can machine learning be applied?What is machine learning useful for?
【FPGA】SDRAM
LeetCode刷题第16天之《239滑动窗口最大值》
【FPGA】名词缩写
无线电射频能量的收集
LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
map和set--天然的搜索和查找语义
.NET Custom Middleware
[C Language] Getting Started
洛谷P2150 寿司晚宴
洛谷P4061 大吉大利,晚上吃鸡
redis按照正则批量删除key
.NET service registration
rub the heat - do not open
Alibaba Cloud releases 3 high-performance computing solutions
洛谷P1196 银河英雄传说
Differences and connections between distributed and clustered
Multi-serial port RS485 industrial gateway BL110
【服务器安装mysql】centos7下使用mysql离线安装包安装mysql5.7