Advertisement

Search a 2D Matrix II - LeetCode 240 Solution

Search a 2D Matrix II - Complete Solution Guide

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

Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix . This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. Example 1: Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 Output: true Example 2: Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,

Detailed Explanation

The problem asks us to search for a specific `target` value within a 2D integer matrix. The matrix has two crucial properties: each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. We need to design an efficient algorithm to determine if the `target` exists in the matrix and return `true` if it does, otherwise `false`. The constraints specify the dimensions of the matrix and the range of values within the matrix and the target.

Solution Approach

The provided solutions use a 'staircase search' or 'ZigZag search' approach, which leverages the sorted nature of both rows and columns. The algorithm starts at the top-right corner of the matrix. If the current element is equal to the target, the search is successful. If the current element is greater than the target, it means the target cannot be in the current column (because all elements below are even larger), so we move to the left. If the current element is smaller than the target, it means the target cannot be in the current row (because all elements to the left are even smaller), so we move down. We repeat this process until the target is found or we go out of bounds.

Step-by-Step Algorithm

  1. Step 1: Check for empty matrix. If the matrix is empty, return `false` immediately.
  2. Step 2: Initialize `row` to 0 (the first row) and `col` to `cols - 1` (the last column).
  3. Step 3: While `row` is within the matrix bounds (`row < rows`) and `col` is within the matrix bounds (`col >= 0`), perform the following:
  4. Step 4: Get the value of the current element `matrix[row][col]`.
  5. Step 5: If `current == target`, return `true`.
  6. Step 6: If `current > target`, it means the target cannot be in the current column, so decrement `col` by 1 (move left).
  7. Step 7: If `current < target`, it means the target cannot be in the current row, so increment `row` by 1 (move down).
  8. Step 8: If the loop finishes without finding the target, return `false`.

Key Insights

  • Insight 1: The sorted nature of rows and columns allows for an efficient search strategy, avoiding a naive O(m*n) traversal.
  • Insight 2: Starting the search from the top-right corner or bottom-left corner allows for eliminating either a row or a column in each comparison.
  • Insight 3: The problem can be viewed as a modified binary search. Instead of bisecting a single array, we are effectively bisecting the search space in two dimensions by either moving to the next row or the previous column

Complexity Analysis

Time Complexity: O(m+n)

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search, Divide and Conquer, Matrix.

Companies

Asked at: ByteDance, Citadel, Coupang, MakeMyTrip, Oracle, PayPal, Snap, Whatnot, tcs.