Skip to content

Commit b823831

Browse files
authored
Create 1329-sort-the-matrix-diagonally.js
1 parent 50991e6 commit b823831

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

1329-sort-the-matrix-diagonally.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number[][]}
4+
*/
5+
const diagonalSort = function(mat) {
6+
const m = mat.length, n = mat[0].length
7+
8+
for(let j = 0; j < n; j++) {
9+
let i = 0, jj = j
10+
const tmp = []
11+
while(jj < n && i < m) {
12+
tmp.push(mat[i++][jj++])
13+
}
14+
tmp.sort((a, b) => a - b)
15+
let idx = 0
16+
jj = j
17+
let ii = 0
18+
while(ii < m && jj < n) {
19+
mat[ii++][jj++] = tmp[idx++]
20+
}
21+
}
22+
23+
for(let i = 1; i < m; i++) {
24+
let j = 0
25+
let ii = i
26+
const tmp = []
27+
while(j < n && ii < m) {
28+
tmp.push(mat[ii++][j++])
29+
}
30+
tmp.sort((a, b) => a - b)
31+
let idx = 0
32+
ii = i
33+
j = 0
34+
while(ii < m && j < n) {
35+
mat[ii++][j++] = tmp[idx++]
36+
}
37+
}
38+
return mat
39+
};

0 commit comments

Comments
 (0)