-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalSortMatrix.java
More file actions
62 lines (53 loc) · 1.96 KB
/
DiagonalSortMatrix.java
File metadata and controls
62 lines (53 loc) · 1.96 KB
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
import java.util.*;
public class DiagonalSortMatrix {
public static int[][] sortDiagonals(int[][] grid) {
int n = grid.length;
// Sort bottom-left triangle (including middle diagonal)
for (int diff = 0; diff < n; diff++) {
List<Integer> diagonal = new ArrayList<>();
for (int i = diff, j = 0; i < n && j < n; i++, j++) {
diagonal.add(grid[i][j]);
}
// Sort in non-increasing order
diagonal.sort((a, b) -> b - a);
// Place back into the grid
for (int i = diff, j = 0, k = 0; i < n && j < n; i++, j++, k++) {
grid[i][j] = diagonal.get(k);
}
}
// Sort top-right triangle
for (int sum = 1; sum < n; sum++) {
List<Integer> diagonal = new ArrayList<>();
for (int i = 0, j = sum; i < n && j < n; i++, j++) {
diagonal.add(grid[i][j]);
}
// Sort in non-decreasing order
Collections.sort(diagonal);
// Place back into the grid
for (int i = 0, j = sum, k = 0; i < n && j < n; i++, j++, k++) {
grid[i][j] = diagonal.get(k);
}
}
return grid;
}
public static void main(String[] args) {
int[][] grid1 = {{1, 7, 3}, {9, 8, 2}, {4, 5, 6}};
int[][] result1 = sortDiagonals(grid1);
System.out.println("Output 1:");
printMatrix(result1);
int[][] grid2 = {{0, 1}, {1, 2}};
int[][] result2 = sortDiagonals(grid2);
System.out.println("Output 2:");
printMatrix(result2);
int[][] grid3 = {{1}};
int[][] result3 = sortDiagonals(grid3);
System.out.println("Output 3:");
printMatrix(result3);
}
// Helper method to print the matrix
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
}