Skip to content

Commit e0078e5

Browse files
author
Bogdan Hladiuc
committed
Day 6 Search in Monotonic matrix Q240
1 parent 2e82356 commit e0078e5

File tree

10 files changed

+339
-2
lines changed

10 files changed

+339
-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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#nullable disable warnings
2+
using System.Diagnostics;
3+
using Library;
4+
5+
namespace Formation {
6+
7+
public class MonotonicMatrix {
8+
public static void Run() {
9+
var test = new Test {
10+
Id = 1,
11+
Matrix = new int[3][] {
12+
new int[] { 0, 0, 0, 1 },
13+
new int[] { 1, 1, 1, 2 },
14+
new int[] { 2, 3, 4, 5 }
15+
},
16+
Expected = true
17+
};
18+
19+
AssortedMethods.PrintInt2DArray(test.Matrix);
20+
21+
var result = Solution(test.Matrix);
22+
23+
test.Verify(result);
24+
}
25+
26+
private static bool Solution(int[][] matrix) {
27+
for (var i = 0; i < matrix.Length; i++) {
28+
for (int j = 0; j < matrix[i].Length; j++) {
29+
if (i > 0 && matrix[i][j] < matrix[i - 1][j]) {
30+
return false;
31+
}
32+
33+
if (j > 0 && matrix[i][j] < matrix[i][j - 1]) {
34+
return false;
35+
}
36+
}
37+
}
38+
39+
return true;
40+
}
41+
}
42+
43+
internal class Test {
44+
public int Id { get; set; }
45+
public int[][] Matrix { get; set; }
46+
public bool Expected { get; set; }
47+
48+
public void Verify(bool result) {
49+
Debug.Assert(result == Expected, $"Test {Id} failed.");
50+
}
51+
}
52+
}
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#nullable disable warnings
2+
using System.Diagnostics;
3+
4+
namespace Formation {
5+
6+
public class SearchInMatrix {
7+
public static void Run() {
8+
var test = new Test {
9+
Id = 1,
10+
Matrix = new int[5][] {
11+
new int[] { 1, 4, 7, 11, 15 },
12+
new int[] { 2, 5, 8, 12, 19 },
13+
new int[] { 3, 6, 9, 16, 22 },
14+
new int[] { 23, 23, 24, 27, 28 },
15+
new int[] { 29, 30, 32, 33, 34 }
16+
},
17+
Expected = true,
18+
K = 5
19+
};
20+
21+
var result = Solution(test.Matrix, test.K);
22+
test.Verify(result);
23+
}
24+
25+
private static bool Solution(int[][] matrix, int k) {
26+
var rows = matrix.Length;
27+
if (rows == 0) {
28+
return false;
29+
}
30+
31+
var cols = matrix[0].Length;
32+
var row = rows - 1;
33+
var col = 0;
34+
35+
while (row >= 0 && col < cols) {
36+
var value = matrix[row][col];
37+
38+
if (value == k) {
39+
return true;
40+
}
41+
42+
if (value < k) {
43+
col++;
44+
} else {
45+
row--;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
}
52+
53+
internal class Test {
54+
public int Id { get; set; }
55+
public int[][] Matrix { get; set; }
56+
public int K { get; set; }
57+
public bool Expected { get; set; }
58+
59+
public void Verify(bool result) {
60+
Debug.Assert(result == Expected, $"Test {Id} failed.");
61+
}
62+
}
63+
}

LeetcodeNew.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_3_Reverse_Groups_of_K_N
9393
EndProject
9494
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_4_Toeplitz_Matrix_Q766", "Formation\21_Days_Challenge\Day_4_Toeplitz_Matrix_Q766\Day_4_Toeplitz_Matrix_Q766.csproj", "{C9FD7A97-4205-421E-A286-3AFE0B2BDA74}"
9595
EndProject
96+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_5_Monotonic_Matrix", "Formation\21_Days_Challenge\Day_5_Monotonic_Matrix\Day_5_Monotonic_Matrix.csproj", "{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}"
97+
EndProject
98+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_6_Search_In_Matrix_Q240", "Formation\21_Days_Challenge\Day_6_Search_In_Matrix_Q240\Day_6_Search_In_Matrix_Q240.csproj", "{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}"
99+
EndProject
96100
Global
97101
GlobalSection(SolutionConfigurationPlatforms) = preSolution
98102
Debug|Any CPU = Debug|Any CPU
@@ -622,12 +626,38 @@ Global
622626
{C9FD7A97-4205-421E-A286-3AFE0B2BDA74}.Release|x64.Build.0 = Release|Any CPU
623627
{C9FD7A97-4205-421E-A286-3AFE0B2BDA74}.Release|x86.ActiveCfg = Release|Any CPU
624628
{C9FD7A97-4205-421E-A286-3AFE0B2BDA74}.Release|x86.Build.0 = Release|Any CPU
629+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
630+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Debug|Any CPU.Build.0 = Debug|Any CPU
631+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Debug|x64.ActiveCfg = Debug|Any CPU
632+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Debug|x64.Build.0 = Debug|Any CPU
633+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Debug|x86.ActiveCfg = Debug|Any CPU
634+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Debug|x86.Build.0 = Debug|Any CPU
635+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Release|Any CPU.ActiveCfg = Release|Any CPU
636+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Release|Any CPU.Build.0 = Release|Any CPU
637+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Release|x64.ActiveCfg = Release|Any CPU
638+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Release|x64.Build.0 = Release|Any CPU
639+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Release|x86.ActiveCfg = Release|Any CPU
640+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18}.Release|x86.Build.0 = Release|Any CPU
641+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
642+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Debug|Any CPU.Build.0 = Debug|Any CPU
643+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Debug|x64.ActiveCfg = Debug|Any CPU
644+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Debug|x64.Build.0 = Debug|Any CPU
645+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Debug|x86.ActiveCfg = Debug|Any CPU
646+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Debug|x86.Build.0 = Debug|Any CPU
647+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Release|Any CPU.ActiveCfg = Release|Any CPU
648+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Release|Any CPU.Build.0 = Release|Any CPU
649+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Release|x64.ActiveCfg = Release|Any CPU
650+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Release|x64.Build.0 = Release|Any CPU
651+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Release|x86.ActiveCfg = Release|Any CPU
652+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A}.Release|x86.Build.0 = Release|Any CPU
625653
EndGlobalSection
626654
GlobalSection(NestedProjects) = preSolution
627655
{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C} = {71C7D88B-6D5D-4E25-A275-869603700831}
628656
{F4D0F4C2-26A6-43AC-BC80-D1762684A605} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
629657
{82FE764B-4AA7-4694-B9BC-85119629AF56} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
630658
{4936F68E-2FB8-4DC3-96CF-53116D2437E6} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
631659
{C9FD7A97-4205-421E-A286-3AFE0B2BDA74} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
660+
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
661+
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
632662
EndGlobalSection
633663
EndGlobal

Run/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ static void Main(string[] args) {
77
// Formation.DeleteMiddleNode.Run(); // Day 1
88
// Formation.SpiralTraversal.Run(); // Day 2
99
// Formation.ReverseKGroup.Run(); // Day 3
10-
Formation.ToeplitzMatrix.Run(); // Day 4
10+
// Formation.ToeplitzMatrix.Run(); // Day 4
11+
// Formation.MonotonicMatrix.Run(); // Day 5
12+
Formation.SearchInMatrix.Run(); // Day 6
1113

1214
#endregion Formation
1315

Run/Run.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_2_Spiral_Traversal_Q54\Day_2_Spiral_Traversal_Q54.csproj" />
4545
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_3_Reverse_Groups_of_K_Nodes_Q25\Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj" />
4646
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_4_Toeplitz_Matrix_Q766\Day_4_Toeplitz_Matrix_Q766.csproj" />
47+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_5_Monotonic_Matrix\Day_5_Monotonic_Matrix.csproj" />
48+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_6_Search_In_Matrix_Q240\Day_6_Search_In_Matrix_Q240.csproj" />
4749
</ItemGroup>
4850

4951
<PropertyGroup>

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,118 @@
284284
}
285285
}
286286
},
287+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_5_Monotonic_Matrix/Day_5_Monotonic_Matrix.csproj": {
288+
"version": "1.0.0",
289+
"restore": {
290+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_5_Monotonic_Matrix/Day_5_Monotonic_Matrix.csproj",
291+
"projectName": "Day_5_Monotonic_Matrix",
292+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_5_Monotonic_Matrix/Day_5_Monotonic_Matrix.csproj",
293+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
294+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_5_Monotonic_Matrix/obj/",
295+
"projectStyle": "PackageReference",
296+
"configFilePaths": [
297+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
298+
],
299+
"originalTargetFrameworks": [
300+
"net6.0"
301+
],
302+
"sources": {
303+
"https://api.nuget.org/v3/index.json": {}
304+
},
305+
"frameworks": {
306+
"net6.0": {
307+
"targetAlias": "net6.0",
308+
"projectReferences": {
309+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
310+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
311+
}
312+
}
313+
}
314+
},
315+
"warningProperties": {
316+
"warnAsError": [
317+
"NU1605"
318+
]
319+
}
320+
},
321+
"frameworks": {
322+
"net6.0": {
323+
"targetAlias": "net6.0",
324+
"imports": [
325+
"net461",
326+
"net462",
327+
"net47",
328+
"net471",
329+
"net472",
330+
"net48"
331+
],
332+
"assetTargetFallback": true,
333+
"warn": true,
334+
"frameworkReferences": {
335+
"Microsoft.NETCore.App": {
336+
"privateAssets": "all"
337+
}
338+
},
339+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
340+
}
341+
}
342+
},
343+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_6_Search_In_Matrix_Q240/Day_6_Search_In_Matrix_Q240.csproj": {
344+
"version": "1.0.0",
345+
"restore": {
346+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_6_Search_In_Matrix_Q240/Day_6_Search_In_Matrix_Q240.csproj",
347+
"projectName": "Day_6_Search_In_Matrix_Q240",
348+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_6_Search_In_Matrix_Q240/Day_6_Search_In_Matrix_Q240.csproj",
349+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
350+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_6_Search_In_Matrix_Q240/obj/",
351+
"projectStyle": "PackageReference",
352+
"configFilePaths": [
353+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
354+
],
355+
"originalTargetFrameworks": [
356+
"net6.0"
357+
],
358+
"sources": {
359+
"https://api.nuget.org/v3/index.json": {}
360+
},
361+
"frameworks": {
362+
"net6.0": {
363+
"targetAlias": "net6.0",
364+
"projectReferences": {
365+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
366+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
367+
}
368+
}
369+
}
370+
},
371+
"warningProperties": {
372+
"warnAsError": [
373+
"NU1605"
374+
]
375+
}
376+
},
377+
"frameworks": {
378+
"net6.0": {
379+
"targetAlias": "net6.0",
380+
"imports": [
381+
"net461",
382+
"net462",
383+
"net47",
384+
"net471",
385+
"net472",
386+
"net48"
387+
],
388+
"assetTargetFallback": true,
389+
"warn": true,
390+
"frameworkReferences": {
391+
"Microsoft.NETCore.App": {
392+
"privateAssets": "all"
393+
}
394+
},
395+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
396+
}
397+
}
398+
},
287399
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
288400
"version": "1.0.0",
289401
"restore": {
@@ -2381,6 +2493,12 @@
23812493
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_4_Toeplitz_Matrix_Q766/Day_4_Toeplitz_Matrix_Q766.csproj": {
23822494
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_4_Toeplitz_Matrix_Q766/Day_4_Toeplitz_Matrix_Q766.csproj"
23832495
},
2496+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_5_Monotonic_Matrix/Day_5_Monotonic_Matrix.csproj": {
2497+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_5_Monotonic_Matrix/Day_5_Monotonic_Matrix.csproj"
2498+
},
2499+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_6_Search_In_Matrix_Q240/Day_6_Search_In_Matrix_Q240.csproj": {
2500+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_6_Search_In_Matrix_Q240/Day_6_Search_In_Matrix_Q240.csproj"
2501+
},
23842502
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
23852503
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
23862504
},

0 commit comments

Comments
 (0)