Find Minimum in rotated Sorted array rotation array for minimum value @LeetCode

Source: Internet
Author: User

O (n) algorithm is not said, this topic mainly examines O (logn) algorithm.


An ordered array is easy to think of using a binary search solution, which is to make some adjustments on a two-point basis. The array can only be flipped once, knowing that the original ordered increment array is divided into two parts, both of which are ordered and incremented (this problem requires only the increment of an ordered array).


If the inverted array is divided into two parts with the X nodes a[0..x] and A[X+1..N]. Then a[0..x] This paragraph is ordered increment, A[X+1..N] This paragraph is also an orderly increment. And because the original array is incremented sequentially, all the numbers in a[0..x] will be greater than any number in A[X+1..N]. So we actually need to find the node a[x+1], the value of this node is the minimum value.


Consider array a[i: j], intermediate node m (m = (i + j)/2).


A[i] < A[j]: The array is incremented, indicating that the X node has been found, and x equals I.


A[i] >= A[j]: The array is not incremented, indicating that the X-node has not been found, then the middle node is compared a[m]


A[M] > A[i]: a[i in the array. M] This paragraph is ordered increment, the flip node x will not be in this paragraph, then we only need to consider A[M+1..J] this paragraph.


A[M] < A[i]: Description Flip node x in A[i. M].


In addition special consideration is the case of only one element.



public class Solution {public    int findmin (int[] num) {                int left = 0;        int right = Num.length-1;                while (left < right)        {            if (Num[left] < Num[right]) {                return num[left];            }                        int mid = left + (right-left)/2;            if (Num[left] <= Num[mid]) {left                = mid + 1,            } else {right                = mid;            }        }        return num[left];}    }

http://orzorz.me/learn/lesson.htm?lessonId=106

Recursive

Thoughts:

  1. If the array just has one element and then return the element.
  2. If the array has both elements, then return the smaller one.
  3. If the left most element was smaller than the right most element and then we can know the array is sorted like never be rotate D. Just return the left one.
  4. By the method of Binary Search, we get the middle element of array, A[mid]. If A[mid] > A[left], then the left half of an array is sorted. We then search for the right half, including A[mid]. Otherwise We search the left half, including A[mid].

public class Solution {public    int findmin (int[] a) {        return helper (A, 0, a.length-1);    }        public static int helper (int[] A, int. left, int.) {        //one element        if (left = right) {            return a[left];        }                 //two elements        if (left = = right-1) {            return a[left]<a[right]? A[left]: A[right];        }                 The array is ordered        if (A[left] < A[right]) {            return a[left];        }                 int mid = (left+right)/2;                 if (A[mid] >= A[left]) {            return helper (A, Mid, right);        } else{            return Helper (A, left, mid);}}}             



https://chesterli0130.wordpress.com/2012/10/20/finding-the-minimum-in-a-sorted-rotated-array/



Find Minimum in rotated Sorted array rotation array for minimum value @LeetCode

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.