diff --git a/3546. Equal Sum Grid Partition I/3546. Equal Sum Grid Partition I.csproj b/3546. Equal Sum Grid Partition I/3546. Equal Sum Grid Partition I.csproj new file mode 100644 index 0000000..9ad7705 --- /dev/null +++ b/3546. Equal Sum Grid Partition I/3546. Equal Sum Grid Partition I.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + _3546._Equal_Sum_Grid_Partition_I + enable + enable + + + diff --git a/3546. Equal Sum Grid Partition I/Program.cs b/3546. Equal Sum Grid Partition I/Program.cs new file mode 100644 index 0000000..47a718f --- /dev/null +++ b/3546. Equal Sum Grid Partition I/Program.cs @@ -0,0 +1,29 @@ +var sol = new Solution(); + +var cases = new (int[][] grid, bool expected, string name)[] +{ + ( + new[] + { + new[] { 1, 4 }, + new[] { 2, 3 } + }, + true, + "Example 1" + ), + ( + new[] + { + new[] { 1, 3 }, + new[] { 2, 4 } + }, + false, + "Example 2" + ) +}; + +foreach (var (grid, expected, name) in cases) +{ + var actual = sol.CanPartitionGrid(grid); + Console.WriteLine($"{name}: {actual} (expected: {expected})"); +} diff --git a/3546. Equal Sum Grid Partition I/Solution.cs b/3546. Equal Sum Grid Partition I/Solution.cs new file mode 100644 index 0000000..7be9006 --- /dev/null +++ b/3546. Equal Sum Grid Partition I/Solution.cs @@ -0,0 +1,49 @@ +public class Solution +{ + struct SumInfo + { + public long Left; + public long Right; + public long Value; + } + + public bool CanPartitionGrid(int[][] grid) + { + var height = grid.Length; + var width = grid[0].Length; + + var colSums = new SumInfo[width]; + var rowSums = new SumInfo[height]; + for (var i = 0; i < height; i++) + { + for (var j = 0; j < grid[i].Length; j++) + { + colSums[j].Value += grid[i][j]; + rowSums[i].Value += grid[i][j]; + + } + rowSums[i].Left = rowSums[i].Value; + if (i > 0) + rowSums[i].Left += rowSums[i - 1].Left; + } + for (var i = height - 1; i >= 0; i--) + { + rowSums[i].Right = rowSums[i].Value; + if (i < height - 1) + rowSums[i].Right += rowSums[i + 1].Right; + } + for (var j = 0; j < width; j++) + { + colSums[j].Left = colSums[j].Value; + if (j > 0) + colSums[j].Left += colSums[j - 1].Left; + } + for (var j = width - 1; j >= 0; j--) + { + colSums[j].Right = colSums[j].Value; + if (j < width - 1) + colSums[j].Right += colSums[j + 1].Right; + } + return rowSums.Any(row => Math.Abs(row.Left - row.Right) == row.Value) || colSums.Any(col => Math.Abs(col.Left - col.Right) == col.Value); + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index f9c5504..6feaf32 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -181,6 +181,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10. Regular Expression Matc EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "2906. Construct Product Matrix", "2906. Construct Product Matrix\2906. Construct Product Matrix.csproj", "{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3546. Equal Sum Grid Partition I", "3546. Equal Sum Grid Partition I\3546. Equal Sum Grid Partition I.csproj", "{8533B947-4274-46C9-8EEA-1439778FEE32}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1259,6 +1261,18 @@ Global {D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x64.Build.0 = Release|Any CPU {D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.ActiveCfg = Release|Any CPU {D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.Build.0 = Release|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x64.ActiveCfg = Debug|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x64.Build.0 = Debug|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x86.ActiveCfg = Debug|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x86.Build.0 = Debug|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Release|Any CPU.Build.0 = Release|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x64.ActiveCfg = Release|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x64.Build.0 = Release|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x86.ActiveCfg = Release|Any CPU + {8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE