[C++] Simple 2 Pointer Solution Explained, ~100% Time, ~95% Space
Nice warm up for the week, this is a nice problem to solve in one simple go.\n\nFirst of all, we are going create a few support variables:\n mr and mc will stor
Nice warm up for the week, this is a nice problem to solve in one simple go.\n\nFirst of all, we are going create a few support variables:\n* `mr` and `mc` will store the number of rows and columns in the provided matrix;\n* `nx` and `ny` are 2 pointers we will use with our newly created array.\n\nWe will then check if the calls we are getting is either illegal (ie: the total number of elements in the new matrix does not match) or if the provided coordinates are identical to the existing one - in either case returning `mat`.\n\nIf that is not the case, we will then create another variable, `res`, a vector of vectors of `r` rows and `c` columns.\n\nNext, we will populate `res` looping with `cy` and `cx` through `mat` and:\n* assigning the currently pointed value (`mat[cy][cx]`) to the matching cell in the new matrix (`res[ny][nx++]`);\n* advance `ny` and reset `nx` to `0` every time we reach the end of the row in the `res` (ie: `nx == c`).\n\nOnce done, we can return `res` :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        // support variables\n        int mr = mat.size(), mc = mat[0].size(), nx = 0, ny = 0;\n        // detecting illegal calls and when the new matrix has the same size\n        if (mr * mc != r * c || mr == r && mc == c) return mat;\n        vector<vector<int>> res(r, vector<int>(c));\n        // populating ress\n        for (int cy = 0; cy < mr; cy++) {\n            for (int cx = 0; cx < mc; cx++) {\n                res[ny][nx++] = mat[cy][cx];\n                // updating nx and ny when we reached the end of a row\n                if (nx == c) nx = 0, ny++;\n            }\n        }\n        return res;\n    }\n};\n```
['Array', 'Two Pointers', 'C', 'C++']