From 556eecbe21baff93985125a3518c49e54ed44ce7 Mon Sep 17 00:00:00 2001 From: Roman Pytkov Date: Mon, 30 Mar 2026 11:17:12 +0300 Subject: [PATCH] 2946. Matrix Similarity After Cyclic Shifts --- ...trix Similarity After Cyclic Shifts.csproj | 11 +++++ .../Program.cs | 46 +++++++++++++++++++ .../Solution.cs | 19 ++++++++ Leetcode.sln | 14 ++++++ 4 files changed, 90 insertions(+) create mode 100644 2946. Matrix Similarity After Cyclic Shifts/2946. Matrix Similarity After Cyclic Shifts.csproj create mode 100644 2946. Matrix Similarity After Cyclic Shifts/Program.cs create mode 100644 2946. Matrix Similarity After Cyclic Shifts/Solution.cs diff --git a/2946. Matrix Similarity After Cyclic Shifts/2946. Matrix Similarity After Cyclic Shifts.csproj b/2946. Matrix Similarity After Cyclic Shifts/2946. Matrix Similarity After Cyclic Shifts.csproj new file mode 100644 index 0000000..7eece3d --- /dev/null +++ b/2946. Matrix Similarity After Cyclic Shifts/2946. Matrix Similarity After Cyclic Shifts.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + _2946._Matrix_Similarity_After_Cyclic_Shifts + enable + enable + + + diff --git a/2946. Matrix Similarity After Cyclic Shifts/Program.cs b/2946. Matrix Similarity After Cyclic Shifts/Program.cs new file mode 100644 index 0000000..b099ed5 --- /dev/null +++ b/2946. Matrix Similarity After Cyclic Shifts/Program.cs @@ -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)}]"; +} diff --git a/2946. Matrix Similarity After Cyclic Shifts/Solution.cs b/2946. Matrix Similarity After Cyclic Shifts/Solution.cs new file mode 100644 index 0000000..bed6a9c --- /dev/null +++ b/2946. Matrix Similarity After Cyclic Shifts/Solution.cs @@ -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; + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 0d16be9..926faa5 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -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