#1901

Find a Peak Element II

specialist · 800 · lc medium +31 · verified · 54.4% accepted · 2,676 likes · top 47%

Description

A peak element in a 2D grid is one that is strictly greater than its up, down, left, and right neighbors. Out-of-bounds neighbors are treated as -1.

Given an m x n matrix mat where no two adjacent cells are equal, find any peak element and return its [row, col] position.

Your solution must run in O(m log n) or O(n log m) time.

Example 1:

Input: mat = [[1,4],[3,2]]
Output: [0,1]
Explanation: Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.

Example 2:

Input: mat = [[10,20,15],[21,30,14],[7,16,32]]
Output: [1,1]
Explanation: Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.

Code

1
2
3