-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathReshapetheMatrix.cpp
More file actions
30 lines (22 loc) · 810 Bytes
/
ReshapetheMatrix.cpp
File metadata and controls
30 lines (22 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Source: https://leetcode.com/problems/reshape-the-matrix/
Time: O(m * n), where m and n are the lengths of rows and columns respectively
Space: O(1), in-place, though we are using extra 2d array(res) but we will still treat space as O(1) and not O(n)
because we usually don't consider the output in the space complexity i.e. only temporary spaces which are used to get the desired output are considered.
*/
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
int m = mat.size();
int n = mat[0].size();
int size = r * c;
if(m * n != size || m == r) {
return mat;
}
vector<vector<int>> res(r, vector<int>(c));
for(int i = 0; i < size; ++i) {
res[i / c][i % c] = mat[i / n][i % n];
}
return res;
}
};