4 DIFFERENT SOLUTIONS IN C++ WITH DIFFERENT TIME COMPLEXITIES:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:\n\n    Integers in each row are sorted in a
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:\n\n    Integers in each row are sorted in ascending from left to right.\n    Integers in each column are sorted in ascending from top to bottom.\n\nExample:\n\nConsider the following matrix:\n\n[\n  [1,   4,  7, 11, 15],\n  [2,   5,  8, 12, 19],\n  [3,   6,  9, 16, 22],\n  [10, 13, 14, 17, 24],\n  [18, 21, 23, 26, 30]\n]\n\nGiven target = 5, return true.\n\nGiven target = 20, return false.\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\nDIVIDE AND CONQUER APPROACH: O(nlogn)\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n```\nclass Solution {\npublic:\n\n    bool searchMatrix(vector<vector<int>>& matrix, int target) {\n        if(matrix.size()==0) return false;\n        if(matrix[0].size()==0) return false;\n        return searchRect(matrix,target,0,0,matrix.size()-1,matrix[0].size()-1);\n    }\n\n    bool searchRect(vector<vector<int>>& matrix, int target, \n                               int top, int left, int bottom, int right) {\n        \n        if(top>bottom || left>right)\n            return false;\n        \n        int x = (top+bottom)/2;\n        int y = (left+right)/2;\n        int center = matrix[x][y];\n        \n        if(center > target){\n            return\n                searchRect(matrix,target,top,left,x-1,right) ||\n                searchRect(matrix,target,x,left,bottom,y-1);\n        }\n        else if(center < target){\n            return\n                searchRect(matrix,target,x+1,left,bottom,right) ||\n                searchRect(matrix,target,top,y+1,x,right);\n        }\n        else\n            return true;\n    }\n};\n```\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\nGREEDY SOLUTION: O(n)\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n```\nclass Solution {\npublic:\n    bool searchMatrix(vector<vector<int>>& matrix, int target) {\n        int m = matrix.size(), n = m ? matrix[0].size() : 0, r = 0, c = n - 1;\n        while (r < m && c >= 0) {\n            if (matrix[r][c] == target) {\n                return true;\n            }\n            matrix[r][c] > target ? c-- : r++;\n        }\n        return false;\n    }\n};\n```\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\nBINARY SEARCH SOLUTION: O(nlogn)\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n```\nclass Solution {\npublic:\n    bool searchMatrix(vector<vector<int>>& matrix, int target) {\n        int m=matrix.size();\n        if(m==0) return false;\n        int n=matrix[0].size();\n        if(n==0) return false;\n        for(int i=0;i<m;i++){\n            if(target<=matrix[i][n-1] and target>=matrix[i][0]){\n                int left=0;\n                int right=n-1;\n                while(left<=right){\n                    int mid=left +(right-left)/2;\n                    if(matrix[i][mid]==target){\n                        return true;\n                    }\n                    else if(target<matrix[i][mid]){\n                        right=mid-1;\n                    }\n                    else left=mid+1;\n                }\n            }\n        }\n        return false;\n    }\n};\n```\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\nBRUTE FORCE SOLUTION: (This method will get you a TLE, but the implementation is correct and is passing all the base cases). O(n^2)\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n```\nclass Solution {\npublic:\n    bool searchMatrix(vector<vector<int>>& matrix, int target) {\n        for(int i=0;i<matrix.size();i++)\n        {\n            for(int j=0;j<matrix[i].size();j++)\n            {\n                if(matrix[i][j]==target)\n                {\n                      return true;\n                }\n            }\n        }\n        return false;\n        \n    }\n};\n```\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[]