Find Peak Element II
Description
Given an integer matrix A
which has the following features :
- The numbers in adjacent positions are different.
- The matrix has
n
rows andm
columns. - For all
i < n
,A[i][0] < A[i][1] && A[i][m - 2] > A[i][m - 1]
. - For all
j < m
,A[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的更多相关文章
- LintCode "Find Peak Element II"
Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 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] ≠ ...
- LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
随机推荐
- DateTimeFormatter 的操作与使用 -- 通俗易懂
在上一章我们讲解了LocalDate.LocalTime.LocalDateTime.Instant的操作与使用,下面讲解它们之间是如何进行格式化 DateTimeFormatter这个类它只提供了时 ...
- 关于工作中.net转java遇到的一个远程调用传递重复参的问题。
工作中遇到一个很奇怪的传参问题.之前.net使用的是一个List列表,列表中有几个重复的参数.列表中使用的model类是KeyValue. 我使用java模仿其写法,传递List和KeyValue.对 ...
- CentOS 7 下安装 MySQL 5.7
从 CentOS 7 系统开始,MariaDB 成为 yum 源中默认的数据库安装包.在 CentOS 7 及以上的系统中使用 yum 安装 MySQL 包将无法使用 MySQL.您可以选择使用完全兼 ...
- FZU2018级算法第五次作业 m_sort(归并排序或线段树求逆序对)
首先对某人在未经冰少允许情况下登录冰少账号原模原样复制其代码并且直接提交的赤裸裸剽窃行为,并且最终被评为优秀作业提出抗议! 题目大意: 给一个数组含n个数(1<=n<=5e5),求使用冒泡 ...
- svn: E230001: Server SSL certificate verification failed: certificate issued
svn: E230001: Server SSL certificate verification failed: certificate issued 今天在使用svn时候发现出现这个问题,这个是因 ...
- Ubuntu 18.04安装arm-linux-gcc交叉编译器
Ubuntu 18.04安装arm-linux-gcc交叉编译器
- 机器学习xgboost参数解释笔记
首先xgboost有两种接口,xgboost自带API和Scikit-Learn的API,具体用法有细微的差别但不大. 在运行 XGBoost 之前, 我们必须设置三种类型的参数: (常规参数)gen ...
- Oracle 11g xe版本---总结1
一.创建用户和授予权限 1.1 环境: Oracle 11g xe 第三方图形客户端: PLSQL Windows 10 必须登录 HR 用户,下面的查询会使用到 HR 中的表. 1.2 SQL 语句 ...
- (转) 嵌入式 Linux 利用 udev 实现自动检测挂载U盘
本文链接:https://blog.csdn.net/cfl927096306/article/details/95180940 udev配置文件是/etc/udev/udev.conf,也许长这样: ...
- mybatis执行DDL语句
对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库进行DDL(c ...