leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE |

commitef7cebfb49bc6b4d1a86f14025c9e270a64530b3
parentdd8db0d2592ad603c959758eee9fe52d3f655f1b
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateWed, 25 Jan 2023 11:15:20 +0100

Data Structure I: Day 4

Diffstat:
AProblems/0566.cpp|++++++++++++++++++++
MREADME.md|+

2 files changed, 21 insertions(+), 0 deletions(-)


diff --git a/Problems/0566.cpp b/Problems/0566.cpp

@@ -0,0 +1,20 @@

class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>> &mat, int r, int c) {
int n = mat.size(), m = mat[0].size();
if (m * n != r * c) return mat;
vector<vector<int>> nmat(r, vector<int>(c));
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
nmat[x][y] = mat[i][j];
y++;
x += y / c;
y %= c;
}
}
return nmat;
}
};

diff --git a/README.md b/README.md

@@ -186,6 +186,7 @@ for solving problems.

| 0559 | Easy | [Maximum Depth of N-ary Tree](Problems/0559.cpp) |
| 0561 | Easy | [Array Partition](Problems/0561.cpp) |
| 0563 | Easy | [Binary Tree Tilt](Problems/0563.cpp) |
| 0566 | Easy | [Reshape the Matrix](Problems/0566.cpp) |
| 0572 | Easy | [Subtree of Another Tree](Problems/0572.cpp) |
| 0589 | Easy | [N-ary Tree Preorder Traversal](Problems/0589.cpp) |
| 0590 | Easy | [N-ary Tree Postorder Traversal](Problems/0590.cpp) |