-
Notifications
You must be signed in to change notification settings - Fork 0
/
SorttheMatrixDiagonally.java
63 lines (54 loc) · 1.39 KB
/
SorttheMatrixDiagonally.java
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
54
55
56
57
58
59
60
61
62
63
package arrays;
import java.util.*;
//1329. Sort the Matrix Diagonally
public class SorttheMatrixDiagonally {
public int[][] diagonalSort(int[][] mat) {
int m=mat.length;
//row wise
int rows=m;
int columns=mat[0].length;
for(int i=0;i<m ;i++){
int temp=i;
int n=mat[i].length;
int j=0;
List<Integer> value= new ArrayList<>();
while(i<m && j<n){
value.add(mat[i][j]);
i++;j++;
}
Collections.sort(value);
i=temp;
j=0;
int index=0;
while(i<m && j<n){
mat[i][j]=value.get(index);
index++;
i++;j++;
}
i=temp;
}
for(int j=1;j<columns; j++){
int row=0;
int temp=j;
List<Integer> value= new ArrayList<>();
while(j<columns&& row<m){
value.add(mat[row][j]);
row++;
j++;
}
Collections.sort(value);
row=0;
j=temp;
int index=0;
while(j<columns&& row<m){
mat[row][j]=value.get(index);
row++;
j++;
index++;
}
//row=0;
j=temp;
}
return mat;
}
}