Skip to content

Commit b661506

Browse files
author
Bogdan Hladiuc
committed
Day 20 Matrix To Zero
1 parent 27bc6d3 commit b661506

File tree

8 files changed

+187
-2
lines changed

8 files changed

+187
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\..\Library\Library.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<TargetFramework>net6.0</TargetFramework>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<Nullable>enable</Nullable>
11+
</PropertyGroup>
12+
13+
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Library;
2+
3+
namespace Formation {
4+
5+
public class MatrixToZero {
6+
public static void Run() {
7+
var matrix = new int[][] {
8+
new int[] { 0, 2, 3 },
9+
new int[] { 4, 5, 6 },
10+
new int[] { 7, 8, 0 }
11+
};
12+
13+
AssortedMethods.PrintInt2DArray(matrix);
14+
15+
var result = Solution(matrix);
16+
17+
AssortedMethods.PrintInt2DArray(matrix);
18+
}
19+
20+
private static int[][] Solution(int[][] matrix) {
21+
var firstRow = false;
22+
var firstCol = false;
23+
24+
if (matrix.Length == 0) {
25+
return matrix;
26+
}
27+
28+
for (var i = 0; i < matrix.Length; i++) {
29+
if (matrix[i][0] == 0) {
30+
firstCol = true;
31+
break;
32+
}
33+
}
34+
35+
for (var i = 0; i < matrix[0].Length; i++) {
36+
if (matrix[0][i] == 0) {
37+
firstRow = true;
38+
break;
39+
}
40+
}
41+
42+
for (var i = 1; i < matrix.Length; i++) {
43+
for (var j = 1; j < matrix[i].Length; j++) {
44+
if (matrix[i][j] == 0) {
45+
matrix[i][0] = 0;
46+
matrix[0][j] = 0;
47+
}
48+
}
49+
}
50+
51+
for (var i = 1; i < matrix.Length; i++) {
52+
for (var j = 1; j < matrix[i].Length; j++) {
53+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
54+
matrix[i][j] = 0;
55+
}
56+
}
57+
}
58+
59+
if (firstRow) {
60+
for (var i = 0; i < matrix[0].Length; i++) {
61+
matrix[0][i] = 0;
62+
}
63+
}
64+
65+
if (firstCol) {
66+
for (var i = 0; i < matrix.Length; i++) {
67+
matrix[i][0] = 0;
68+
}
69+
}
70+
71+
return matrix;
72+
}
73+
}
74+
}

LeetcodeNew.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_18_K_Closest_Points", "
123123
EndProject
124124
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_19_Text_Justification", "Formation\21_Days_Challenge\Day_19_Text_Justification\Day_19_Text_Justification.csproj", "{A60BC75A-CFB2-4DD0-ABB6-996B1A7A365E}"
125125
EndProject
126+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_20_Set_Matrix_To_Zero", "Formation\21_Days_Challenge\Day_20_Set_Matrix_To_Zero\Day_20_Set_Matrix_To_Zero.csproj", "{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}"
127+
EndProject
126128
Global
127129
GlobalSection(SolutionConfigurationPlatforms) = preSolution
128130
Debug|Any CPU = Debug|Any CPU
@@ -832,6 +834,18 @@ Global
832834
{A60BC75A-CFB2-4DD0-ABB6-996B1A7A365E}.Release|x64.Build.0 = Release|Any CPU
833835
{A60BC75A-CFB2-4DD0-ABB6-996B1A7A365E}.Release|x86.ActiveCfg = Release|Any CPU
834836
{A60BC75A-CFB2-4DD0-ABB6-996B1A7A365E}.Release|x86.Build.0 = Release|Any CPU
837+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
838+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Debug|Any CPU.Build.0 = Debug|Any CPU
839+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Debug|x64.ActiveCfg = Debug|Any CPU
840+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Debug|x64.Build.0 = Debug|Any CPU
841+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Debug|x86.ActiveCfg = Debug|Any CPU
842+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Debug|x86.Build.0 = Debug|Any CPU
843+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Release|Any CPU.ActiveCfg = Release|Any CPU
844+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Release|Any CPU.Build.0 = Release|Any CPU
845+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Release|x64.ActiveCfg = Release|Any CPU
846+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Release|x64.Build.0 = Release|Any CPU
847+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Release|x86.ActiveCfg = Release|Any CPU
848+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40}.Release|x86.Build.0 = Release|Any CPU
835849
EndGlobalSection
836850
GlobalSection(NestedProjects) = preSolution
837851
{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C} = {71C7D88B-6D5D-4E25-A275-869603700831}
@@ -854,5 +868,6 @@ Global
854868
{C4C60E4F-3C29-4890-A838-5D3A307D3BEE} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
855869
{83EB3558-B244-4F7F-ABAE-BE642A703A43} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
856870
{A60BC75A-CFB2-4DD0-ABB6-996B1A7A365E} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
871+
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
857872
EndGlobalSection
858873
EndGlobal

Run/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ static void Main(string[] args) {
2222
// Formation.DaysToHigherTemp.Run(); // Day 16
2323
// Formation.MinimumSumTriangle.Run(); // Day 17
2424
// Formation.KClosestPoints.Run(); // Day 18
25-
Formation.TextJustification.Run(); // Day 19
25+
// Formation.TextJustification.Run(); // Day 19
26+
Formation.MatrixToZero.Run(); // Day 20
2627

2728
#endregion Formation
2829

Run/Run.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_17_Minimum_Sum_Triangle\Day_17_Minimum_Sum_Triangle.csproj" />
6060
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_18_K_Closest_Points\Day_18_K_Closest_Points.csproj" />
6161
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_19_Text_Justification\Day_19_Text_Justification.csproj" />
62+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_20_Set_Matrix_To_Zero\Day_20_Set_Matrix_To_Zero.csproj" />
6263
</ItemGroup>
6364

6465
<PropertyGroup>

Run/obj/Run.csproj.nuget.dgspec.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,62 @@
676676
}
677677
}
678678
},
679+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj": {
680+
"version": "1.0.0",
681+
"restore": {
682+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj",
683+
"projectName": "Day_20_Set_Matrix_To_Zero",
684+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj",
685+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
686+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/obj/",
687+
"projectStyle": "PackageReference",
688+
"configFilePaths": [
689+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
690+
],
691+
"originalTargetFrameworks": [
692+
"net6.0"
693+
],
694+
"sources": {
695+
"https://api.nuget.org/v3/index.json": {}
696+
},
697+
"frameworks": {
698+
"net6.0": {
699+
"targetAlias": "net6.0",
700+
"projectReferences": {
701+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
702+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
703+
}
704+
}
705+
}
706+
},
707+
"warningProperties": {
708+
"warnAsError": [
709+
"NU1605"
710+
]
711+
}
712+
},
713+
"frameworks": {
714+
"net6.0": {
715+
"targetAlias": "net6.0",
716+
"imports": [
717+
"net461",
718+
"net462",
719+
"net47",
720+
"net471",
721+
"net472",
722+
"net48"
723+
],
724+
"assetTargetFallback": true,
725+
"warn": true,
726+
"frameworkReferences": {
727+
"Microsoft.NETCore.App": {
728+
"privateAssets": "all"
729+
}
730+
},
731+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
732+
}
733+
}
734+
},
679735
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj": {
680736
"version": "1.0.0",
681737
"restore": {
@@ -3242,6 +3298,9 @@
32423298
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj": {
32433299
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj"
32443300
},
3301+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj": {
3302+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj"
3303+
},
32453304
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj": {
32463305
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj"
32473306
},

Run/obj/project.assets.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@
158158
"bin/placeholder/Day_1_Remove_Delete_Middle_Node_Q2095.dll": {}
159159
}
160160
},
161+
"Day_20_Set_Matrix_To_Zero/1.0.0": {
162+
"type": "project",
163+
"framework": ".NETCoreApp,Version=v6.0",
164+
"dependencies": {
165+
"Library": "1.0.0"
166+
},
167+
"compile": {
168+
"bin/placeholder/Day_20_Set_Matrix_To_Zero.dll": {}
169+
},
170+
"runtime": {
171+
"bin/placeholder/Day_20_Set_Matrix_To_Zero.dll": {}
172+
}
173+
},
161174
"Day_2_Spiral_Traversal_Q54/1.0.0": {
162175
"type": "project",
163176
"framework": ".NETCoreApp,Version=v6.0",
@@ -768,6 +781,11 @@
768781
"path": "../Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj",
769782
"msbuildProject": "../Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj"
770783
},
784+
"Day_20_Set_Matrix_To_Zero/1.0.0": {
785+
"type": "project",
786+
"path": "../Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj",
787+
"msbuildProject": "../Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj"
788+
},
771789
"Day_2_Spiral_Traversal_Q54/1.0.0": {
772790
"type": "project",
773791
"path": "../Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj",
@@ -1013,6 +1031,7 @@
10131031
"Day_18_K_Closest_Points >= 1.0.0",
10141032
"Day_19_Text_Justification >= 1.0.0",
10151033
"Day_1_Remove_Delete_Middle_Node_Q2095 >= 1.0.0",
1034+
"Day_20_Set_Matrix_To_Zero >= 1.0.0",
10161035
"Day_2_Spiral_Traversal_Q54 >= 1.0.0",
10171036
"Day_3_Reverse_Groups_of_K_Nodes_Q25 >= 1.0.0",
10181037
"Day_4_Toeplitz_Matrix_Q766 >= 1.0.0",
@@ -1122,6 +1141,9 @@
11221141
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj": {
11231142
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj"
11241143
},
1144+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj": {
1145+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_20_Set_Matrix_To_Zero/Day_20_Set_Matrix_To_Zero.csproj"
1146+
},
11251147
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj": {
11261148
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj"
11271149
},

Run/obj/project.nuget.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 2,
3-
"dgSpecHash": "Uz3FBHwMNle8laNAYAcTwgtv+Q3J2N4+Ku3KMY6BlO3ZCRKTOqZkQgHb2UupRawMGujf5MP3SGlg+nJO0nbFnw==",
3+
"dgSpecHash": "ePrTNxuRNvNb9tPXb8YndOZkw1Yg63RnaKQ/HEpS5NrtBWa24hpLSqJTABI9Z/yS03prbHeqa39/j8Cx5tv4nw==",
44
"success": true,
55
"projectFilePath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Run/Run.csproj",
66
"expectedPackageFiles": [],

0 commit comments

Comments
 (0)