One minute
Leetcode 1329
1329. Sort the Matrix Diagonally
The idea of this solution is kind of hard to explain so I will show it with the following images:

i = len(mat), andj = len(mat[0])
The yellow part is every where
iandjwill iterate
the diagonal lines are in red

Before we continue you might be confused about why we switch
2and1even thoughi = 1andj = 1. This can be shown with the example array[3, 2, 1]we can pretend that3, 2, 1is diagonal:
3
2
1
When we switch 3 and 2
2
3
1
Then when we switch 3 and 1
2
1
3
This is not sorted, so we have to switch 1 and 2
1
2
3

The Code:
func diagonalSort(mat [][]int) [][]int {
for i := 0; i < len(mat)-1; i++ {
for j := 0; j < len(mat[0])-1; j++ {
newI := i + 1
newJ := j + 1
for k := 1; k <= min(newI, newJ); k++ {
if mat[newI-k][newJ-k] > mat[newI-k+1][newJ-k+1] {
mat[newI-k][newJ-k], mat[newI-k+1][newJ-k+1] =
mat[newI-k+1][newJ-k+1], mat[newI-k][newJ-k]
}
}
}
}
return mat
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
Read other posts