diff --git a/C++/DiagonalSortMatrix.cpp b/C++/DiagonalSortMatrix.cpp new file mode 100644 index 0000000..8943b1a --- /dev/null +++ b/C++/DiagonalSortMatrix.cpp @@ -0,0 +1,58 @@ +//https://leetcode.com/problems/sort-the-matrix-diagonally/ +class Solution +{ +public: + vector> diagonalSort(vector> &mat) + { + int n = mat.size(); + int m = mat[0].size(); + int x, y, counter; + for (int i = 1; i <= m; i++) + { + vector val; + x = i; + y = 0; + while (x < m && y < n) + { + val.push_back(mat[y][x]); + x++; + y++; + } + sort(val.begin(), val.end()); + x = i; + y = 0; + counter = 0; + while (x < m && y < n) + { + mat[y][x] = val[counter]; + counter++; + x++; + y++; + } + } + for (int i = 1; i <= n; i++) + { + vector val; + x = 0; + y = i; + while (x < m && y < n) + { + val.push_back(mat[y][x]); + x++; + y++; + } + sort(val.begin(), val.end()); + x = 0; + y = i; + counter = 0; + while (x < m && y < n) + { + mat[y][x] = val[counter]; + counter++; + x++; + y++; + } + } + return mat; + } +};