Description

Given an integer matrix A which has the following features :

  • The numbers in adjacent positions are different.
  • The matrix has n rows and m columns.
  • For all i < nA[i][0] < A[i][1] && A[i][m - 2] > A[i][m - 1].
  • For all j < mA[0][j] < A[1][j] && A[n - 2][j] > A[n - 1][j]

We define a position [i, j] is a peak if:

  A[i][j] > A[i + 1][j] && A[i][j] > A[i - 1][j] &&
A[i][j] > A[i][j + 1] && A[i][j] > A[i][j - 1]
Find a peak element in this matrix. Return the index of the peak.
Guarantee that there is at least one peak, and if there are multiple peaks, return any one of them.

Example

Example 1:

Input:
[
[1, 2, 3, 6, 5],
[16,41,23,22, 6],
[15,17,24,21, 7],
[14,18,19,20,10],
[13,14,11,10, 9]
]
Output: [1,1]
Explanation: [2,2] is also acceptable. The element at [1,1] is 41, greater than every element adjacent to it.

Example 2:

Input:
[
[1, 5, 3],
[4,10, 9],
[2, 8, 7]
]
Output: [1,1]
Explanation: There is only one peek.

Challenge

Solve it in O(n+m) time.

If you come up with an algorithm that you thought it is O(nlogm) or O(mlogn), can you prove it is actually O(n+m) or propose a similar but O(n+m) algorithm?

思路:http://courses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf 或者使用二分(时间复杂度为O(nlog(n)).

// O(n + m) 解法
class Solution {
/**
* @param A: An integer matrix
* @return: The index of the peak
*/
public List<Integer> find(int x1, int x2, int y1, int y2, int[][] A) { int mid1 = x1 + (x2 - x1) / 2;
int mid2 = y1 + (y2 - y1) / 2; int x = mid1, y = mid2;
int max = A[mid1][mid2];
for (int i = y1; i <= y2; ++i)
if (A[mid1][i] > max) {
max = A[mid1][i];
x = mid1;
y = i;
} for (int i = x1; i <= x2; ++i)
if (A[i][mid2] > max) {
max = A[i][mid2];
x = i;
y = mid2;
} boolean isPeak = true;
if (A[x - 1][y] > A[x][y]) {
isPeak = false;
x -= 1;
} else if (A[x + 1][y] > A[x][y]) {
isPeak = false;
x += 1;
} else if (A[x][y - 1] > A[x][y]) {
isPeak = false;
y -= 1;
} else if (A[x][y + 1] > A[x][y]) {
isPeak = false;
y += 1;
} if (isPeak) {
return new ArrayList<Integer>(Arrays.asList(x, y));
} if (x >= x1 && x < mid1 && y >= y1 && y < mid2) {
return find(x1, mid1 - 1, y1, mid2 - 1, A);
} if (x >= 1 && x < mid1 && y > mid2 && y <= y2) {
return find(x1, mid1 - 1, mid2 + 1, y2, A);
} if (x > mid1 && x <= x2 && y >= y1 && y < mid2) {
return find(mid1 + 1, x2, y1, mid2 - 1, A);
} if (x >= mid1 && x <= x2 && y > mid2 && y <= y2) {
return find(mid1 + 1, x2, mid2 + 1, y2, A);
} return new ArrayList<Integer>(Arrays.asList(-1, -1)); } public List<Integer> findPeakII(int[][] A) {
// write your code here
int n = A.length;
int m = A[0].length;
return find(1, n - 2, 1, m - 2, A);
}
} class Solution {
/**
* @param A: An integer matrix
* @return: The index of the peak
*/
public List<Integer> findPeakII(int[][] A) {
// this is the nlog(n) method
int low = 1, high = A.length - 2;
List<Integer> ans = new ArrayList<Integer>();
while (low <= high) {
int mid = (low + high) / 2;
int col = find(mid, A);
if (A[mid][col] < A[mid - 1][col]) {
high = mid - 1;
} else if (A[mid][col] < A[mid + 1][col]) {
low = mid + 1;
} else {
ans.add(mid);
ans.add(col);
break;
}
}
return ans;
} int find(int row, int[][] A) {
int col = 0;
for (int i = 0; i < A[row].length; i++) {
if (A[row][i] > A[row][col]) {
col = i;
}
}
return col;
}
}

  

 

Find Peak Element II的更多相关文章

  1. LintCode "Find Peak Element II"

    Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...

  2. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  3. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  4. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  5. [LintCode] Find Peak Element 求数组的峰值

    There is an integer array which has the following features: The numbers in adjacent positions are di ...

  6. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  7. 【leetcode】Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  8. Java for LeetCode 162 Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  9. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

随机推荐

  1. [PDF] - 获取 RadioButtonList 控件值的方法

    背景 目标是通过 iTextSharp 读取 PDF 模板,填充内容后以生成新 PDF 文件.利用 福昕PDF编辑器个人版 可以获取到 RadioButtonList 的组名,但是获取不到每一个 Ra ...

  2. linux服务器搭建lnmp php 微擎环境备用

    以前的时候装个php环境各种的配置麻烦啊,于是乎我就像搜搜一键安装php环境,果然 lamp 和phpstudy 两个环境软件都支持,最后发现lamp 还合胃口就选择了lamp https://lnm ...

  3. 《Mysql - 在Mysql服务出现瓶颈时,有哪些“饮鸩止渴”提高性能的方法?》

    一:情景 - 业务高峰期,生产环境的 MySQL 压力太大,没法正常响应,需要短期内.临时性地提升一些性能. - 在业务高发时候,Mysql 服务压力过大,导致业务受损, 用户的开发负责人说,不管你用 ...

  4. 邮件标准协议:MIME(Multipurpose Internet Mail Extensions)

    MIME(多用途互联网邮件扩展)指的是一系列电子邮件技术规范 ,主要包括 RFC 2045~2049   传统的电子邮件只能使用 ASCII 字符,导致非英文字符都不能在电子邮件中使用 而且电子邮件中 ...

  5. 游记-NOI2019

    Day -18 被各路julao们轮番吊打-- Day -12 鸽子F发布了笔试题库,然而并没有 "MLE全场记零分" 的操作 Day -8 广二体育馆机器装配完毕,误闯开幕式表演 ...

  6. idea 中 下载源码:Sources not download for:

    使用idea 下载源码出现:Sources not found for: 解决方案:在对应的pom.xml 文件中打开 terminal,执行 mvn命令: mvn dependency:source ...

  7. Tomcat一闪而过的调试方法

    很少用tomcat来部署,都是用springboot微服务.只是以前学的时候搞demo试过而已. 软件测试的期末作业要求要测一个Javaweb的项目,给了一个包然后要求部署在tomcat中并启动. 然 ...

  8. jQuery 事件介绍

    什么是事件?页面对不同访问者的响应叫做事件.事件处理程序指的是当 HTML 中发生某些事件时所调用的方法. 常用的时间主要有以下几种: click()事件:click() 方法是当按钮点击事件被触发时 ...

  9. 外汇MT4编程手册

    1.为了最大的方便用户,交易中断的一些变量可以从智能系统输入. AccountNumber-账号(同义词:AccNum) Ask –卖价(买方出价) Balance – 交易账户的余额值 Bars – ...

  10. Thymeleaf 模板

    Thymeleaf 模板布局 th:fragment.th:replace.th:insert.th:remove th:fragment  模板布局 模板片段说明 模板中,经常希望从其他模板中包含⼀ ...