C++ search from top-right
Search from the top-right element and reduce the search space by one row or column at each time.\n\n\n[[ 1,  4,  7, 11, 15],\n [ 2,  5,  8, 12, 19], \n [ 3,  6,
Search from the **top-right** element and reduce the search space by one row or column at each time.\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\nSuppose we want to search for `12` in the above matrix. compare `12` with the top-right element `nums[0][4] = 15`. Since `12 < 15`, `12` cannot appear in the column of `15` since all elements in that column are greater than or equal to `15`. Now we reduce the search space by one column (the last column).\n\nWe further compare `12` with the top-right element of the remaining matrix, which is `nums[0][3] = 11`. Since `12 > 11`, `12` cannot appear in the row of `11` since all elements in this row are less than or equal to `11` (the last column has been discarded). Now we reduce the search space by one row (the first row).\n\nWe move on to compare `12` with the top-right element of the remaining matrix, which is `nums[1][3] = 12`. Since it is equal to `12`, we return `true`.\n\n```cpp\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```
['Matrix', 'C++']