2946. Matrix Similarity After Cyclic Shifts

This commit is contained in:
2026-03-30 11:17:12 +03:00
parent 2cac465442
commit 556eecbe21
4 changed files with 90 additions and 0 deletions

View File

@@ -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>

View 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)}]";
}

View 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;
}
}

View File

@@ -199,6 +199,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "30. Substring with Concaten
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3548. Equal Sum Grid Partition II", "3548. Equal Sum Grid Partition II\3548. Equal Sum Grid Partition II.csproj", "{D4C2B514-1A13-495A-8627-8B40AD8B2770}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "2946. Matrix Similarity After Cyclic Shifts", "2946. Matrix Similarity After Cyclic Shifts\2946. Matrix Similarity After Cyclic Shifts.csproj", "{8BC4770E-4B28-4AE4-8066-3609E4298363}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -1385,6 +1387,18 @@ Global
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|x64.Build.0 = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|x86.ActiveCfg = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|x86.Build.0 = Release|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Debug|x64.ActiveCfg = Debug|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Debug|x64.Build.0 = Debug|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Debug|x86.ActiveCfg = Debug|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Debug|x86.Build.0 = Debug|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Release|Any CPU.Build.0 = Release|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Release|x64.ActiveCfg = Release|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Release|x64.Build.0 = Release|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Release|x86.ActiveCfg = Release|Any CPU
{8BC4770E-4B28-4AE4-8066-3609E4298363}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE