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. SCI-hub使用技巧(下载外文文献)

    下载外文文献方法指南: (1)首先查找需要下载文献的DOI (2)在Sci-Hub主页搜索框输入URL.DOI或者PMID. (3)点击open即可看见下载界面. 参考文献:https://mp.we ...

  2. 二维码制作分享-Python

    分享一个简单快捷的二维码制作,Python实现. 1.安装准备 已安装的Python+Pycharm的计算机.本人win7+Python3.6+Pycharm 2.库包下载安装 Python二维码制作 ...

  3. Bad owner or permissions on .ssh/config win10问题解决

    最近向系统添加了新用户账号后出现了问题,尝试使用私钥登陆服务器,提示了 Bad owner or permissions on .ssh/config 这个报错,就是如题中的问题 修复 按照Windo ...

  4. 深夜扒一扒Android的发展史

    说道,Android的发展史,我们就不得不先来了解一下手机的发展史 Android之前的时代 1831年英国的法拉第发现了电磁感应现象,麦克斯韦进一步用数学公式阐述了法拉第等人的研究成果,并把电磁感应 ...

  5. Spring Cloud 基于Consul 实现配置服务

    Spring Cloud体系中提供了Config组件来进行配置服务管理.而Consul除了提供服务注册与发现功能外,同时也提供配置管理功能.本位将介绍如何结合Spring Cloud + Consul ...

  6. 在论坛中出现的比较难的sql问题:18(字符合并 整数解析星期几)

    原文:在论坛中出现的比较难的sql问题:18(字符合并 整数解析星期几) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得 ...

  7. C#中using的用途

    using System;                                                    --主命名空间,包含所有.net基础类型和通用类型,比如Object, ...

  8. 字节数组(byte[])与16进制字符串转换

    /// <summary> /// 转换扩展类 /// </summary> public static class ConvertExtend { /// <summa ...

  9. 【转载】 C#中使用Sum方法对List集合进行求和操作

    在C#的List操作中,有时候我们需要对List集合对象的某个属性进行求和操作,此时可以使用Lambda表达式中的Sum方法来快速实现此求和操作,使用Sum方法可使代码简洁易读,并且省去写for循环或 ...

  10. node中用的cookie-parser插件设置的max-age,和普通正常设置max-age的计算方式不一样

    在cookie-parser中通过max-age设置的cookie的过期时间是按照毫秒计算的; 在普通设置的时候max-age后面的值是按秒计算的;