From 5e4ba0d992f3697816a8c2c0d1da5100d51400e0 Mon Sep 17 00:00:00 2001 From: Ripunjay Narula Date: Sun, 31 Oct 2021 01:59:41 +0530 Subject: [PATCH] Create DiagonalSortMatrix.cpp --- C++/DiagonalSortMatrix.cpp | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 C++/DiagonalSortMatrix.cpp 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; + } +};