class Solution {
public int[][] diagonalSort(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
for (int col = 0; col < n; col++) {
sortDiagonal(mat, 0, col, m, n);
}
for (int row = 1; row < m; row++) {
sortDiagonal(mat, row, 0, m, n);
}
return mat;
}
private void sortDiagonal(int[][] mat, int row, int col, int m, int n) {
List<Integer> diag = new ArrayList<>();
int r = row, c = col;
while (r < m && c < n) {
diag.add(mat[r][c]);
r++;
c++;
}
Collections.sort(diag);
r = row;
c = col;
int idx = 0;
while (r < m && c < n) {
mat[r][c] = diag.get(idx++);
r++;
c++;
}
}
}