2946. Matrix Similarity After Cyclic Shifts
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>_2946._Matrix_Similarity_After_Cyclic_Shifts</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
46
2946. Matrix Similarity After Cyclic Shifts/Program.cs
Normal file
46
2946. Matrix Similarity After Cyclic Shifts/Program.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
var sol = new Solution();
|
||||
|
||||
var cases = new (int[][] mat, int k, bool expected)[]
|
||||
{
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 1, 2, 3 },
|
||||
new[] { 4, 5, 6 },
|
||||
new[] { 7, 8, 9 }
|
||||
},
|
||||
4,
|
||||
false
|
||||
),
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 1, 2, 1, 2 },
|
||||
new[] { 5, 5, 5, 5 },
|
||||
new[] { 6, 3, 6, 3 }
|
||||
},
|
||||
2,
|
||||
true
|
||||
),
|
||||
(
|
||||
new[]
|
||||
{
|
||||
new[] { 2, 2 },
|
||||
new[] { 2, 2 }
|
||||
},
|
||||
3,
|
||||
true
|
||||
)
|
||||
};
|
||||
|
||||
foreach (var (mat, k, expected) in cases)
|
||||
{
|
||||
var actual = sol.AreSimilar(mat, k);
|
||||
Console.WriteLine($"mat = {FormatMatrix(mat)}, k = {k} -> {actual} (expected: {expected})");
|
||||
}
|
||||
|
||||
static string FormatMatrix(int[][] mat)
|
||||
{
|
||||
var rows = mat.Select(row => $"[{string.Join(",", row)}]");
|
||||
return $"[{string.Join(",", rows)}]";
|
||||
}
|
||||
19
2946. Matrix Similarity After Cyclic Shifts/Solution.cs
Normal file
19
2946. Matrix Similarity After Cyclic Shifts/Solution.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
public class Solution
|
||||
{
|
||||
public bool AreSimilar(int[][] mat, int k)
|
||||
{
|
||||
var answer = true;
|
||||
var shift = -k;
|
||||
for (var i = 0; i < mat.Length; i++)
|
||||
{
|
||||
var len = mat[i].Length;
|
||||
for (var j = 0; j < len; j++)
|
||||
{
|
||||
var idx = ((j + shift) % len + len) % len;
|
||||
answer &= mat[i][j] == mat[i][idx];
|
||||
}
|
||||
shift = -shift;
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user