algorithm 1.4.19 Local minimum of a matrix
来源:互联网 发布:aframe.js下载 编辑:程序博客网 时间:2024/06/05 08:34
题目:
Given anN-by-Narray a[] of N 2 distinct integers, design an algorithm that runs in time proportional to N to find a local minimum:a pair of indicesi 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 toN 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/
0 0
- algorithm 1.4.19 Local minimum of a matrix
- Algs4-1.4.18 Find local minimum in n x n matrix in O(n) time
- A non local algorithm for image denoising
- A non-local algorithm for image denoising
- Implementation of Strassen’s Algorithm for Matrix Multiplication
- 【论文】A Minimum Distortion Noise Reduction Algorithm With Multiple Microphones
- Local Minimum in a array using lg N
- 【A Neural Algorithm of Artistic Style】 Pics
- A Neural Algorithm of Artistic Style
- A Neural Algorithm of Artistic Style
- A Neural Algorithm of Artistic Style
- A Neural Algorithm of Artistic Style
- 《A Neural Algorithm of Artistic Style》笔记
- A Neural Algorithm of Artistic Style
- A neural algorithm of artistic style
- a summary of local feature and decriptor
- Minimum Depth of a Binary Tree
- Minimum Path Sum of a Binary Tree
- tomcat服务器https配置及证书生成(笔记)
- OpenGL缓冲区对象之EBO
- Java 异步回调
- uboot的编译及连接过程
- webpack工作流程分析
- algorithm 1.4.19 Local minimum of a matrix
- appium FAQ
- Eclipse NEON.1 安装最新的CDT插件
- 01UI-day6-160104
- 东软ACM题目的点点滴滴
- w3school之HTML002
- 使用sqlite在unity中创建数据库
- 01UI-day7-160105
- python pands实现execl转csv 并修改csv指定列