Search a 2D Matrix II LeetCode Solution

Last updated on October 25th, 2024 at 10:35 pm

Here, We see Search a 2D Matrix II LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Binary Search, Divide-and-Conquer

Companies

Amazon, Apple, Google

Level of Question

Medium

Search a 2D Matrix II LeetCode Solution

Search a 2D Matrix II LeetCode Solution

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:

searchgrid2

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:

searchgrid

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 = 20
Output: false

1. Search a 2D Matrix II LeetCode Solution C++

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size(), n = m ? matrix[0].size() : 0, r = 0, c = n - 1;
        while (r < m && c >= 0) {
            if (matrix[r][c] == target) {
                return true;
            }
            matrix[r][c] > target ? c-- : r++;
        }
        return false;
    }
};

2. Search a 2D Matrix II LeetCode Solution Java

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix == null || matrix.length < 1 || matrix[0].length <1) {
            return false;
        }
        int col = matrix[0].length-1;
        int row = 0;
        while(col >= 0 && row <= matrix.length-1) {
            if(target == matrix[row][col]) {
                return true;
            } else if(target < matrix[row][col]) {
                col--;
            } else if(target > matrix[row][col]) {
                row++;
            }
        }
        return false;
    }
}

3. Search a 2D Matrix II Solution JavaScript

var searchMatrix = function(matrix, target) {
     if(!matrix || !matrix.length) return false;
    const rows = matrix.length;
    const cols = matrix[0].length;
    function hasTarget(startRow, endRow, startCol, endCol) {
        if(startRow > endRow || startCol > endCol) return false;
        const middleRow = Math.floor((endRow - startRow) / 2) + startRow;
        const middleCol = Math.floor((endCol - startCol) / 2) + startCol;
        if(matrix[middleRow][middleCol] === target) return true;
        if (matrix[middleRow][middleCol] < target) {
            return hasTarget(middleRow + 1, endRow, startCol, endCol) ||
                   hasTarget(startRow, middleRow, middleCol + 1, endCol);
        } else {
            return hasTarget(startRow, endRow, startCol, middleCol - 1) ||
                   hasTarget(startRow, middleRow - 1, middleCol, endCol);
        }
    }
    return hasTarget(0, rows - 1, 0, cols - 1);
}

4. Search a 2D Matrix II Solution Python

class Solution(object):
    def searchMatrix(self, matrix, target):
        y, i, j = len(matrix), 0, len(matrix[0]) - 1
        while i < y and ~j:
            cell = matrix[i][j]
            if cell == target: return True
            elif cell > target: j -= 1
            else: i += 1
        return False
Scroll to Top