Loading problem...
A matrix diagonal is a sequence of cells that starts from any cell in the first row or first column and extends towards the bottom-right corner of the matrix, advancing one row down and one column right at each step until it reaches the boundary of the matrix. For instance, in a 6 x 3 matrix, the diagonal beginning at cell mat[2][0] includes the cells mat[2][0], mat[3][1], and mat[4][2].
You are given a two-dimensional integer matrix mat with dimensions m x n. Your task is to independently arrange the elements along each diagonal in non-decreasing order (from smallest to largest) and return the resulting transformed matrix.
Each diagonal operates independently — rearranging one diagonal should not affect the elements of any other diagonal. The elements within each diagonal should flow from the top-left cell toward the bottom-right cell in sorted (ascending) order.
mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]][[1,1,1,1],[1,2,2,2],[1,2,3,3]]The matrix has 3 rows and 4 columns. Each diagonal is sorted independently: the main diagonal [3,2,1] becomes [1,2,3], and similarly all other diagonals are sorted. The final matrix preserves the diagonal structure but with elements ordered from smallest to largest along each diagonal path.
mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]][[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]This is a 5x6 matrix with multiple diagonals. Each diagonal line from top-left to bottom-right is sorted independently. For example, the main diagonal [11,55,36,25,5] becomes [5,11,25,36,55], which can be seen tracing from mat[0][0] down to mat[4][4] in the output.
mat = [[4,2],[3,1]][[1,2],[3,4]]A simple 2x2 matrix has two diagonals of length 2 and two diagonals of length 1. The main diagonal [4,1] becomes [1,4], and the single-element diagonals [2] and [3] remain unchanged. The result shows all elements properly sorted along their respective diagonals.
Constraints