题目:
Given an N-by-N array a[] of N 2 distinct integers, design an algorithm that runs in time proportional to N to find a local minimum:a pair of indices i and j such that a[i][j] < a[i+1][j], a[i][j] < a[i][j+1],a[i][j] < a[i-1][j], and a[i][j] < a[i][j-1]. The running time of your program should be proportional to N in the worst case.
思路:
数组范围为arry[left,right,up,down];
p = (left+right)/2, q = (up+down)/2;在 arry[i][p] 和arry[q][i]上面找到最小值mv;
然后判断最大值mv是不是符合条件,如果不合法,那么看一下mv上下左右四个值的大小关系,从而决定下一次寻找是在那个象限内;
具体算法如下:
package com.frozenxia.algorithm.basic.exercises;
public class LocalMinimun1419 {
public int findMinimun(int[][] arr, int left, int right, int up, int down) {
int p = (left + right) / 2;
int mc = Integer.MAX_VALUE;
int mi = 0;
int mj = 0;
for (int i = up; i <= down; i++) {
if (mc > arr[p][i]) {
mc = arr[p][i];
mi = p;
mj = i;
}
}
int q = (up + down) / 2;
for (int i = left; i <= right; i++) {
if (mc > arr[i][q]) {
mc = arr[i][q];
mi = i;
mj = p;
}
}
int vl = (Integer) (mi - 1 < left ? Integer.MAX_VALUE : arr[mi - 1][mj]);
int vr = (Integer) (mi + 1 > left ? Integer.MAX_VALUE : arr[mi + 1][mj]);
int vu = (Integer) (mj - 1 < up ? Integer.MAX_VALUE : arr[mi][mj - 1]);
int vd = (Integer) (mj + 1 > down ? Integer.MAX_VALUE : arr[mi][mj + 1]);
if (mc <= vl && mc <= vr) {
if (vd < mc) {
if (mi <= p) {
return findMinimun(arr, left, p - 1, q + 1, down);// 3
} else {
return findMinimun(arr, p + 1, right, q + 1, down);// 4
}
} else if (vu < mc) {
if (mi <= p) {
return findMinimun(arr, left, p - 1, up, q - 1);// 1
} else {
return findMinimun(arr, p + 1, right, up, q - 1);// 2
}
}
} else {
if (vl < mc) {
if (mj <= q) {
return findMinimun(arr, left, p - 1, up, q - 1);// 1
} else {
return findMinimun(arr, left, p - 1, q + 1, down);// 3
}
} else if (vr < mc) {
if (mj <= p) {
return findMinimun(arr, p + 1, right, up, q - 1);// 2
} else {
return findMinimun(arr, p + 1, right, q + 1, down);// 4
}
}
}
return mc;
}
public static void main(String[]args) {
int[][] problem2 = { { 0, 0, 9, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 8, 9 },
{ 0, 2, 0, 0, 0, 0, 0 }, { 0, 3, 0, 0, 0, 0, 0 }, { 0, 5, 0, 0, 0, 0, 0 }, { 0, 4, 7, 0, 0, 0, 0 } };
int [][]problem1 = {{7,8,9},{6,1,5},{3,4,2}};
int [][]problem = problem1;
System.out.println(new LocalMinimun1419().findMinimun(problem, 0, problem[0].length-1, 0, problem.length-1));
}
}
相关资料:
https://courses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf
http://www.filipekberg.se/2014/02/10/understanding-peak-finding/