Advertisement

Find a Peak Element II - LeetCode 1901 Solution

Find a Peak Element II - Complete Solution Guide

Find a Peak Element II is LeetCode problem 1901, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbors to the left, right, top, and bottom. Given a 0-indexed m x n matrix mat where no two adjacent cells are equal , find any peak element mat[i][j] and return the length 2 array [i,j] . You may assume that the entire matrix is surrounded by an outer perimeter with the value -1 in each cell. You must write an algorithm that runs in O(m log(n)) or O(n log(m)) time. Example 1: Input: mat = [[1,4],[3,2]

Detailed Explanation

The problem asks us to find a peak element in a 2D matrix. A peak element is defined as an element that is strictly greater than all its adjacent neighbors (left, right, top, and bottom). The matrix has the property that no two adjacent cells are equal, and the entire matrix is surrounded by an outer perimeter of -1. We need to return the row and column indices of any peak element, and the algorithm must run in O(m log n) or O(n log m) time.

Solution Approach

The provided solution uses a binary search approach on the columns of the matrix. For each column selected by the binary search, it finds the maximum element in that column. Then, it checks if this maximum element is greater than its left and right neighbors. If it is, we have found a peak and we return its coordinates. If the left neighbor is greater, it means there must be a peak to the left, so we narrow the search to the left half of the matrix. If the right neighbor is greater, it means there must be a peak to the right, so we narrow the search to the right half of the matrix.

Step-by-Step Algorithm

  1. Step 1: Initialize `low_col` to 0 and `high_col` to n - 1, representing the boundaries of the column indices.
  2. Step 2: While `low_col` is less than or equal to `high_col`, perform a binary search:
  3. Step 3: Calculate the middle column index `mid_col` as the average of `low_col` and `high_col`.
  4. Step 4: Find the maximum element in the `mid_col` column and its row index `max_row`.
  5. Step 5: Get the value of the current element `current_val` at `mat[max_row][mid_col]`, the left neighbor `left_val` (or -1 if there's no left neighbor), and the right neighbor `right_val` (or -1 if there's no right neighbor).
  6. Step 6: If `current_val` is greater than both `left_val` and `right_val`, then `mat[max_row][mid_col]` is a peak element. Return `[max_row, mid_col]`.
  7. Step 7: If `left_val` is greater than `current_val`, there must be a peak to the left. Set `high_col = mid_col - 1`.
  8. Step 8: Otherwise, `right_val` must be greater than `current_val`, indicating there must be a peak to the right. Set `low_col = mid_col + 1`.

Key Insights

  • Insight 1: The constraint that no two adjacent cells are equal ensures that a peak always exists. This makes the problem solvable without explicitly checking for its existence.
  • Insight 2: Binary search can be applied on either rows or columns to reduce the search space in each iteration.
  • Insight 3: Instead of checking all neighbors explicitly, focus on finding the maximum element in each column and comparing it with its left and right neighbors in the same row to determine a potential peak.

Complexity Analysis

Time Complexity: O(m log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search, Matrix.

Companies

Asked at: Zeta.