Most optimal solution with explanation | Eliminating the rows and cols (concept of binary search)
\n\n# Approach\n1. Start from the top-right corner of the matrix (row = 0, col = m-1), where n is the number of rows and m is the number of columns in the matri
\n\n# Approach\n1. Start from the top-right corner of the matrix (row = 0, col = m-1), where n is the number of rows and m is the number of columns in the matrix.\n2. Compare the element at the current position (matrix[row][col]) with the target value:\n    - If the element is equal to the target, return true.\n    - If the element is less than the target, move to the next row (row++) to explore larger elements.\n    - If the element is greater than the target, move to the previous column (col--) to explore smaller elements.\n3. Continue the process until either the target is found, or the search goes out of bounds (row >= n or col < 0).\n4. If the target is not found in the matrix, return false.\n\n# Complexity\n- Time complexity:\nO(m+n)\n\n- Space complexity:\nO(1)\n\n```C++ []\nclass Solution {\npublic:\n    bool searchMatrix(vector<vector<int>>& matrix, int target) {\n        int n = matrix.size();\n        int m = matrix[0].size();\n        int row = 0, col = m-1;\n        while(row < n && col >= 0) {\n            if (matrix[row][col] == target) return true;\n            else if (matrix[row][col] < target) row++;\n            else col--;\n        }\n        return false;\n    }\n};\n```\n```JAVA []\nclass Solution {\n    public boolean searchMatrix(int[][] matrix, int target) {\n        int n = matrix.length;\n        int m = matrix[0].length;\n        int row = 0, col = m - 1;\n        \n        while (row < n && col >= 0) {\n            if (matrix[row][col] == target) {\n                return true;\n            } else if (matrix[row][col] < target) {\n                row++;\n            } else {\n                col--;\n            }\n        }\n        \n        return false;\n    }\n}\n\n```\n```Python []\nclass Solution:\n    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n        n = len(matrix)\n        m = len(matrix[0])\n        row, col = 0, m - 1\n        \n        while row < n and col >= 0:\n            if matrix[row][col] == target:\n                return True\n            elif matrix[row][col] < target:\n                row += 1\n            else:\n                col -= 1\n        \n        return False\n\n```\n
['Array', 'Binary Search', 'Divide and Conquer', 'Matrix', 'Python', 'C++', 'Java', 'Python3']