Day i do not know

This commit is contained in:
Electrominch
2022-10-18 00:45:31 +03:00
parent 01c5720116
commit 284b6ff10c
72 changed files with 1256 additions and 12 deletions

View File

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

15
542. 01 Matrix/Program.cs Normal file
View File

@@ -0,0 +1,15 @@
namespace _542._01_Matrix;
internal class Program
{
static void Main(string[] args)
{
int[][] mat = new int[][] { new int[] { 0,0,0, },
new int[] { 0,1,0, },
new int[] { 1,1,1, },
};
var res = new Solution().UpdateMatrix(mat);
foreach(var row in res)
Console.WriteLine(String.Join(" ", row));
}
}

View File

@@ -0,0 +1,35 @@
namespace _542._01_Matrix;
public class Solution
{
public int[][] UpdateMatrix(int[][] mat)
{
bool hasChanges = true;
while(hasChanges)
{
hasChanges = false;
for (int y = 0; y < mat.Length; y++)
{
for (int x = 0; x < mat[y].Length; x++)
{
if (mat[y][x] == 0)
continue;
int min = int.MaxValue;
for (int i = 1; i < 9; i += 2)
{
int newY = i / 3 + y - 1;
int newX = i % 3 + x - 1;
if (newY >= 0 && newY < mat.Length && newX >= 0 && newX < mat[newY].Length)
{
min = Math.Min(min, mat[newY][newX]);
}
}
if(mat[y][x] != min + 1)
hasChanges = true;
mat[y][x] = min + 1;
}
}
}
return mat;
}
}