-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
44 lines (44 loc) · 1.06 KB
/
Solution.cs
File metadata and controls
44 lines (44 loc) · 1.06 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
public class Solution
{
public int[][] SortMatrix(int[][] grid)
{
int n = grid.Length;
// top-right
// j-i = const
for (int c = 1; c < n; c++)
{
List<int> tmp = [];
for (int i = 0; i + c < n; i++)
{
int j = i + c;
tmp.Add(grid[i][j]);
}
tmp.Sort((a, b) => a - b);
int idx = 0;
for (int i = 0; i + c < n; i++)
{
int j = i + c;
grid[i][j] = tmp[idx++];
}
}
// bottom-left
// i-j = const
for (int c = 0; c < n; c++)
{
List<int> tmp = [];
for (int i = c; i < n; i++)
{
int j = i - c;
tmp.Add(grid[i][j]);
}
tmp.Sort((a, b) => b - a);
int idx = 0;
for (int i = c; i < n; i++)
{
int j = i - c;
grid[i][j] = tmp[idx++];
}
}
return grid;
}
}