3546. Equal Sum Grid Partition I

This commit is contained in:
2026-03-25 12:43:02 +03:00
parent a3131bf6d0
commit d99b7d18e3
4 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>_3546._Equal_Sum_Grid_Partition_I</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

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

View File

@@ -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);
}
}

View File

@@ -181,6 +181,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10. Regular Expression Matc
EndProject 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}" 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 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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|x64.Build.0 = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.ActiveCfg = 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 {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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE