Advertisement

Search a 2D Matrix - LeetCode 74 Solution

Search a 2D Matrix - Complete Solution Guide

Search a 2D Matrix is LeetCode problem 74, 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

You are given an m x n integer matrix matrix with the following two properties: Each row is sorted in non-decreasing order. The first integer of each row is greater than the last integer of the previous row. Given an integer target , return true if target is in matrix or false otherwise . You must write a solution in O(log(m * n)) time complexity. Example 1: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true Example 2: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30

Detailed Explanation

The problem asks us to search for a specific integer (`target`) within a 2D matrix (`matrix`). This matrix has two key properties: (1) each row is sorted in non-decreasing order, and (2) the first integer of each row is greater than the last integer of the previous row. We need to return `true` if the `target` exists in the matrix, and `false` otherwise. Crucially, the solution must have a time complexity of O(log(m*n)), where m is the number of rows and n is the number of columns.

Solution Approach

The solution uses binary search to efficiently find the `target` within the 2D matrix. By treating the matrix as a sorted 1D array, we can apply binary search. The algorithm iteratively narrows down the search range by comparing the middle element with the `target`. The core idea is to convert the 1D middle index `mid_idx` to its corresponding row and column indices in the 2D matrix using `row = mid_idx // n` and `col = mid_idx % n`, respectively. This allows us to access the element at `matrix[row][col]` and compare it with the target.

Step-by-Step Algorithm

  1. Step 1: Initialize `left` to 0 and `right` to `m * n - 1`, representing the start and end indices of the flattened 2D matrix.
  2. Step 2: While `left` is less than or equal to `right`, calculate the middle index `mid_idx = (left + right) // 2`.
  3. Step 3: Convert the `mid_idx` to row and column indices using `row = mid_idx // n` and `col = mid_idx % n`, where `n` is the number of columns.
  4. Step 4: Get the value at `matrix[row][col]`. If this value is equal to the `target`, return `true`.
  5. Step 5: If the value at `matrix[row][col]` is less than the `target`, update `left = mid_idx + 1` to search in the right half.
  6. Step 6: If the value at `matrix[row][col]` is greater than the `target`, update `right = mid_idx - 1` to search in the left half.
  7. Step 7: If the loop finishes without finding the `target`, return `false`.

Key Insights

  • Insight 1: The sorted nature of rows and the increasing order between rows allows us to treat the 2D matrix as a single, sorted 1D array.
  • Insight 2: Binary search is the most suitable algorithm to achieve the required O(log(m*n)) time complexity.
  • Insight 3: Instead of explicitly creating a 1D array, we can use index manipulation to map 1D indices to 2D row and column indices during the binary search.

Complexity Analysis

Time Complexity: O(log(m*n))

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search, Matrix.

Companies

Asked at: Accenture, Adobe, Amazon, Apple, Arista Networks, Bloomberg, Cisco, Coupang, Goldman Sachs, Google, Grab, Meta, Microsoft, Nutanix, Snap, Tinkoff, Visa, Walmart Labs, Yahoo, Yandex.