2906. Construct Product Matrix
This commit is contained in:
@@ -1,2 +1,74 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
static string FormatMatrix(int[][] matrix)
|
||||
{
|
||||
var rows = new string[matrix.Length];
|
||||
for (var i = 0; i < matrix.Length; i++)
|
||||
rows[i] = "[" + string.Join(",", matrix[i]) + "]";
|
||||
return "[" + string.Join(",", rows) + "]";
|
||||
}
|
||||
|
||||
var sol = new Solution();
|
||||
|
||||
var cases = new (int[][] input, string expected)[]
|
||||
{
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 1, 2 },
|
||||
new[] { 3, 4 }
|
||||
},
|
||||
"[[24,12],[8,6]]"
|
||||
),
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 12345 },
|
||||
new[] { 2 },
|
||||
new[] { 1 }
|
||||
},
|
||||
"[[2],[0],[0]]"
|
||||
),
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 4, 8, 8 },
|
||||
new[] { 6, 2, 5 },
|
||||
new[] { 7, 3, 7 },
|
||||
new[] { 6, 3, 5 }
|
||||
},
|
||||
"[[3525,7935,7935],[6465,7050,2820],[7305,585,7305],[6465,585,2820]]"
|
||||
),
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 4, 3, 9 },
|
||||
new[] { 3, 9, 10 },
|
||||
new[] { 9, 7, 8 },
|
||||
new[] { 8, 4, 7 },
|
||||
new[] { 6, 1, 3 }
|
||||
},
|
||||
"[[3255,225,75],[225,75,11178],[75,1860,7800],[7800,3255,1860],[6285,675,225]]"
|
||||
),
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new []{1,2,2},
|
||||
new []{1,4,3}
|
||||
},
|
||||
"[[48,24,24],[48,12,16]]"
|
||||
),
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 414750857 },
|
||||
new[] { 449145368 },
|
||||
new[] { 767292749 }
|
||||
},
|
||||
"[[1462],[3103],[9436]]"
|
||||
)
|
||||
};
|
||||
|
||||
foreach (var (input, expected) in cases)
|
||||
{
|
||||
var actual = sol.ConstructProductMatrix(input);
|
||||
Console.WriteLine($"{FormatMatrix(input)} -> {FormatMatrix(actual)} (expected: {expected})");
|
||||
}
|
||||
|
||||
80
2906. Construct Product Matrix/Solution.cs
Normal file
80
2906. Construct Product Matrix/Solution.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
public class Solution
|
||||
{
|
||||
struct MulInfo
|
||||
{
|
||||
public long Left;
|
||||
public long Right;
|
||||
}
|
||||
|
||||
|
||||
public int[][] ConstructProductMatrix(int[][] grid)
|
||||
{
|
||||
var mod = 12345;
|
||||
var table = new MulInfo[grid.Length][];
|
||||
|
||||
var lastRow = grid.Length - 1;
|
||||
var lastCol = grid[0].Length - 1;
|
||||
|
||||
// прямой проход
|
||||
for (var i = 0; i < grid.Length; i++)
|
||||
{
|
||||
table[i] = new MulInfo[grid[i].Length];
|
||||
for (var j = 0; j < grid[i].Length; j++)
|
||||
{
|
||||
if (j == 0 && i == 0)
|
||||
{
|
||||
table[i][j].Left = grid[i][j] % mod;
|
||||
}
|
||||
else if (j > 0)
|
||||
{
|
||||
table[i][j].Left = table[i][j - 1].Left * grid[i][j] % mod;
|
||||
}
|
||||
else if (i > 0)
|
||||
{
|
||||
table[i][j].Left = table[i - 1][lastCol].Left * grid[i][j];
|
||||
table[i][j].Left %= mod;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// обратный проход
|
||||
for (var i = lastRow; i >= 0; i--)
|
||||
{
|
||||
for (var j = lastCol; j >= 0; j--)
|
||||
{
|
||||
if (j == lastCol && i == lastRow)
|
||||
{
|
||||
table[i][j].Right = grid[i][j] % mod;
|
||||
}
|
||||
else if (j < lastCol)
|
||||
{
|
||||
table[i][j].Right = table[i][j + 1].Right * grid[i][j] % mod;
|
||||
}
|
||||
else if (i < lastRow)
|
||||
{
|
||||
table[i][j].Right = table[i + 1][0].Right * grid[i][j] % mod;
|
||||
}
|
||||
|
||||
var left = 1L;
|
||||
var right = 1L;
|
||||
if (i == 0 && j == 0)
|
||||
left = 1;
|
||||
else if (j > 0)
|
||||
left = table[i][j - 1].Left;
|
||||
else if (i > 0)
|
||||
left = table[i - 1][lastCol].Left;
|
||||
|
||||
if (i == lastRow && j == lastCol)
|
||||
right = 1;
|
||||
else if (j < lastCol)
|
||||
right = table[i][j + 1].Right;
|
||||
else if (i < lastRow)
|
||||
right = table[i + 1][0].Right;
|
||||
grid[i][j] = (int)(left * right % mod);
|
||||
}
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user