-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
43 lines (42 loc) · 1.38 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
using System;
using System.Linq;
namespace FindValidMatrix
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
static int[][] RestoreMatrix(int[] rowSum, int[] colSum)
{
var res = new int[rowSum.Length][];
for (int i = 0; i < res.Length; i++)
res[i] = new int[colSum.Length];
var count = rowSum.Count(x => x != 0) + colSum.Count(x => x != 0);
while (count != 0)
{
int r = 0, rowMin = int.MaxValue;
int c = 0, colMin = int.MaxValue;
for (int i = 0; i < rowSum.Length; i++)
{
if (rowSum[i] == 0 || rowSum[i] >= rowMin) continue;
rowMin = rowSum[i];
r = i;
}
for (int i = 0; i < colSum.Length; i++)
{
if (colSum[i] == 0 || colSum[i] >= colMin) continue;
colMin = colSum[i];
c = i;
}
res[r][c] = Math.Min(rowMin, colMin);
rowSum[r] -= Math.Min(rowMin, colMin);
colSum[c] -= Math.Min(rowMin, colMin);
if (rowSum[r] == 0) count--;
if (colSum[c] == 0) count--;
}
return res;
}
}
}