Created
August 9, 2016 14:36
-
-
Save gfwfail/b5346bf7a306735beacaa69081b5c4ce to your computer and use it in GitHub Desktop.
findlocalminimum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
static int findLocalMinimum(int[] a) { | |
if (a[0] < a[1]) { | |
return 0; | |
} | |
if (a[a.length - 1] < a[a.length - 2]) { | |
return a.length - 1; | |
} | |
int s = 0; | |
int count = 0; | |
int e = a.length - 1; | |
int mid = (s + e) / 2; | |
while (s <= e) { | |
count++; | |
System.out.printf("s:%d e:%d m:%d\n", s, e, mid); | |
System.out.printf("%d %d %d\n", a[s], a[e], a[mid]); | |
System.out.println("Count:" + count); | |
mid = s + (e - s) / 2; | |
if ((a[mid] < a[mid + 1]) && (a[mid] < a[mid - 1])) { | |
return mid; | |
} | |
if ((a[mid] < a[mid + 1])) | |
e = mid - 1; | |
else s = mid + 1; | |
} | |
return -1; | |
} | |
public static void main(String[] args) { | |
int x[] = {9, 1, 2, 3, 4, 5, 3, 1, 2, 3, 4, 5, 9}; | |
System.out.println(findLocalMinimum(x)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment