-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
49 lines (48 loc) · 1.59 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
namespace SortTheMatrixDiagonally
{
class Program
{
static void Main(string[] args)
{
int[][] mat = new int[3][];
mat[0] = new int[] { 3, 3, 1, 1 };
mat[1] = new int[] { 2, 2, 1, 2 };
mat[2] = new int[] { 1, 1, 1, 2 };
Console.WriteLine(DiagonalSort(mat));
}
static int[][] DiagonalSort(int[][] mat)
{
for (int r = 0; r < mat.Length; r++)
{
var record = new List<int>();
int pointer = 0;
while (r + pointer < mat.Length && pointer < mat[0].Length)
{
record.Add(mat[r + pointer][pointer]);
pointer++;
}
record.Sort();
pointer = 0;
while (r + pointer < mat.Length && pointer < mat[0].Length)
mat[r + pointer][pointer] = record[pointer++];
}
for (int c = 1; c < mat[0].Length; c++)
{
var record = new List<int>();
int pointer = 0;
while (pointer < mat.Length && pointer + c < mat[0].Length)
{
record.Add(mat[pointer][pointer + c]);
pointer++;
}
record.Sort();
pointer = 0;
while (pointer < mat.Length && pointer + c < mat[0].Length)
mat[pointer][pointer + c] = record[pointer++];
}
return mat;
}
}
}