"Leetcode" Find Minimum in rotated Sorted Array Java implementation

Source: Internet
Author: User
Tags throw exception

I. Description of the topic

Suppose a sorted array is rotated on some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).

Find the minimum element.

Assume no duplicate exists in the array.

Second, analysis

This is difficult because the default is that all the elements in the array are different. There are two types of situations:

1. All elements in the array are monotonically incremented (0 1 2 4 5 6 7)

2, there is a rotation point of (4 5 6 7 0 1 2), it depends on the position of the median value is in the preceding increment sequence or in the subsequent increment sequence

Third, design ideas

1, set two pointers, initial state the first pointer points to the first element of the preceding sub-array, the second pointer to the last element of the back-face array;

2. Find the middle element of two pointers;

3, if it is greater than or equal to the first pointer to the element, then it is in the preceding sub-array, and obviously the smallest element on the right side of the middle element, if it is less than equal to the second pointer to the element, it is in the subsequent sub-array, and clearly the smallest element on the left side of the middle element.

Package Com.edu.leetcode;public class Findminimuminrotatedsortedarray {public int findmin (int[] num) {if (num.length==1 ) {return num[0];} int I=0;int j=num.length-1;int mid=0;if (Num[i]<num[j]) {                        ///monotonically incrementing the case return num[0];} else{//has a inflection point while (i+1!=j) {mid= (i+j)/2;if (Num[mid]>num[i]) {//indicates that the position of the mid is i=mid in the preceding increment sequence;} if (Num[mid]<num[j]) {//indicates that the position of the mid is j=mid in the subsequent increment sequence;}} return num[j];}     } public static void Main (string[] args) {//TODO auto-generated method Stubfindminimuminrotatedsortedarray f= New Findminim Uminrotatedsortedarray (); int[] num={4,5,6}; SYSTEM.OUT.PRINTLN (F.findmin (num));}}

  

Third, difficulty improvement--when there are duplicate elements allowed

Analysis:

The first solution: brute force--traverse the entire array to get the minimum value

The second solution: half-search-the idea is roughly the same as above, but the difference is that when the I,mid and J position three elements are equal, there is no way to use the half method, only to traverse the entire array

#include <iostream> #include <stdlib.h> #include <stack>using namespace std;int min (int arry[],int len )//Returns the minimum number of coordinates {if (arry==null| |    len<=0) return-1;    int start=0;    int end=len-1;        while (start<end) {//If the first element is less than the last element, the array is sorted.        if (Arry[start]<arry[end]) return start;        When the start pointer and end pointer are next to each other, the return end pointer is the smallest element's coordinate if (end-start==1) return end;        int mid= (start+end)/2;        If Arry[mid],arry[start] and Arry[end] Three numbers are equal, you can only use order to find if (Arry[mid]==arry[start]&&arry[mid]==arry[end])            {int index=start;            for (int i=start+1;i<=end;i++) {if (Arry[i]<arry[index]) index=i;        } return index;        }//If the intermediate element is less than the end element, then the intermediate element is indicated in the second half of the array, modifying the end pointer if (Arry[mid]<arry[end]) {end=mid;       }//If the intermediate element is greater than the first element, then the intermediate element is in the first half of the array, modify the start pointer else if (Arry[mid]>arry[start]) {start=mid; }} return-1;} int minnum (int arry[],int len)//Returns the value of the minimum number {if (arry==null| |    len<=0) Throw exception ("illegal input");    int start=0;    int end=len-1;        while (Arry[start]>=arry[end]) {if (end-start==1)//If Start and end difference 1 returns Arry[end] return arry[end];        int mid= (start+end)/2;        If Arry[mid],arry[start] and Arry[end] Three numbers are equal, you can only use order to find if (Arry[mid]==arry[start]&&arry[mid]==arry[end])            {int Result=arry[start];            for (int i=start+1;i<=end;i++) {if (arry[i]<result) result=arry[i];        } return result;        if (Arry[mid]>=arry[start])//If the middle element is greater than the first element, move the first pointer {start=mid;        } else if (Arry[mid]<=arry[end]) {end=mid; }} return arry[start];//if the first arry[start]<arry[end] indicates that the array is a sorted array, returns arry[start]}void main () {int arry[]={1,0,1,1,1}    ; int Len=sizeof (ARry)/sizeof (int);    int index=min (Arry,len);    int Minnum=minnum (Arry,len);    cout<< "minimum number in array position:" <<index<<endl;    cout<< "Minimum number of values:" <<minnum<<endl; System ("Pause");}

  

"Leetcode" Find Minimum in rotated Sorted Array Java implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.