-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSorttheMatrixDiagonally.js
55 lines (45 loc) · 1.06 KB
/
SorttheMatrixDiagonally.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Leetcode - Sort the Matrix Diagonally
/**
https://leetcode.com/problems/sort-the-matrix-diagonally/
*/
/**
* @param {number} x
* @param {number} y
* @param {number} height
* @param {number} width
* @param {number[][]} mat
* @return {void}
*/
var sort = function(x, y, height, width, mat) {
let currentArr = [];
let currentY = y;
let currentX = x;
while (currentY < height && currentX < width) {
currentArr.push(mat[currentY][currentX]);
currentY++;
currentX++;
}
currentArr.sort((a, b) => a - b);
let i = currentArr.length - 1;
while(i >= 0){
currentY--;
currentX--;
mat[currentY][currentX] = currentArr[i];
i--;
}
}
/**
* @param {number[][]} mat
* @return {number[][]}
*/
var diagonalSort = function(mat) {
const height = mat.length;
const width = mat[0].length;
for(let i = 0; i < height; i++){
sort(0, i, height, width, mat)
}
for(let j = 1; j < width; j++){
sort(j, 0, height, width, mat)
}
return mat;
};