Skip to content

Instantly share code, notes, and snippets.

@gfwfail
Created August 9, 2016 14:36
Show Gist options
  • Save gfwfail/b5346bf7a306735beacaa69081b5c4ce to your computer and use it in GitHub Desktop.
Save gfwfail/b5346bf7a306735beacaa69081b5c4ce to your computer and use it in GitHub Desktop.
findlocalminimum
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