Skip to content

Commit 22d947c

Browse files
author
Bogdan Hladiuc
committed
Formation 21 Daily Challenges
1 parent b3e0efa commit 22d947c

File tree

13 files changed

+432
-28
lines changed

13 files changed

+432
-28
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"git.alwaysShowStagedChangesResourceGroup": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
<RootNamespace>Day_1_Remove_Delete_Middle_Node_2095_</RootNamespace>
10+
<ImplicitUsings>enable</ImplicitUsings>
11+
<Nullable>enable</Nullable>
12+
</PropertyGroup>
13+
14+
</Project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#nullable disable warnings
2+
using System.Diagnostics;
3+
using Library;
4+
5+
namespace Formation {
6+
7+
public class DeleteMiddleNode {
8+
9+
public static void Run() {
10+
var tests = new Test[] {
11+
new Test {
12+
Id = 1,
13+
Input = new SingleLinkedListNode(1,
14+
new SingleLinkedListNode(2,
15+
new SingleLinkedListNode(3,
16+
null))),
17+
Expected = new SingleLinkedListNode(1,
18+
new SingleLinkedListNode(3, null))
19+
},
20+
new Test {
21+
Id = 2,
22+
Input = new SingleLinkedListNode(1,
23+
new SingleLinkedListNode(2,
24+
new SingleLinkedListNode(3,
25+
new SingleLinkedListNode(4, null)))),
26+
Expected = new SingleLinkedListNode(1,
27+
new SingleLinkedListNode(2,
28+
new SingleLinkedListNode(4, null)))
29+
}
30+
};
31+
32+
foreach (var test in tests) {
33+
var result = Solution(test.Input);
34+
test.Verify(result);
35+
}
36+
}
37+
38+
private static SingleLinkedListNode Solution(SingleLinkedListNode list) {
39+
if (list == null) {
40+
return list;
41+
}
42+
43+
var result = new SingleLinkedListNode(int.MinValue, list);
44+
var slow = list;
45+
var fast = list?.Next;
46+
47+
while (fast != null) {
48+
slow = slow.Next;
49+
fast = fast.Next?.Next;
50+
}
51+
52+
slow.Next = slow.Next?.Next;
53+
54+
return result.Next;
55+
}
56+
}
57+
58+
internal class Test {
59+
public int Id { get; set; }
60+
public SingleLinkedListNode Input { get; set; }
61+
public SingleLinkedListNode Expected { get; set; }
62+
63+
public void Verify(SingleLinkedListNode result) {
64+
Debug.Assert(CheckList(result), $"Test {Id} failed.");
65+
}
66+
67+
private bool CheckList(SingleLinkedListNode result) {
68+
while (result != null && Expected != null && result.Val == Expected.Val) {
69+
result = result.Next;
70+
Expected = Expected.Next;
71+
}
72+
73+
return result != null || Expected != null;
74+
}
75+
}
76+
}
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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#nullable disable warnings
2+
using System.Diagnostics;
3+
using Library;
4+
5+
namespace Formation {
6+
7+
public class SpiralTraversal {
8+
public static void Run() {
9+
var test = new Test {
10+
Id = 1,
11+
InputMatrix = new int[4][] {
12+
new int[] { 1, 2, 3, 4 },
13+
new int[] { 5, 6, 7, 8 },
14+
new int[] { 9, 10, 11, 12 },
15+
new int[] { 13, 14, 15, 16 }
16+
},
17+
Expected = new int[] { 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 }
18+
};
19+
20+
AssortedMethods.PrintInt2DArray(test.InputMatrix);
21+
22+
var result = Solution(test.InputMatrix);
23+
AssortedMethods.PrintIntArray(result);
24+
test.Verify(result);
25+
}
26+
27+
private static int[] Solution(int[][] matrix) {
28+
var result = new int[matrix.Length * matrix[0].Length];
29+
var rStart = 0;
30+
var rEnd = matrix.Length - 1;
31+
var cStart = 0;
32+
var cEnd = matrix[0].Length - 1;
33+
34+
var direction = 0; // 0: right; 1: down; 2 = left; 3 = up
35+
var idx = 0;
36+
37+
while (idx < result.Length) {
38+
39+
switch (direction) {
40+
case 0: // left to right
41+
for (var i = cStart; i <= cEnd; i++) {
42+
result[idx++] = matrix[rStart][i];
43+
}
44+
rStart++;
45+
break;
46+
case 1:
47+
for (var i = rStart; i <= rEnd; i++) {
48+
result[idx++] = matrix[i][cEnd];
49+
}
50+
cEnd--;
51+
break;
52+
case 2:
53+
for (var i = cEnd; i >= cStart; i--) {
54+
result[idx++] = matrix[rEnd][i];
55+
}
56+
rEnd--;
57+
break;
58+
case 3:
59+
for (var i = rEnd; i >= rStart; i--) {
60+
result[idx++] = matrix[i][cStart];
61+
}
62+
cStart++;
63+
break;
64+
}
65+
66+
direction = (direction + 1) % 4;
67+
}
68+
69+
return result;
70+
}
71+
}
72+
73+
internal class Test {
74+
public int Id { get; set; }
75+
public int[][] InputMatrix { get; set; }
76+
public int[] Expected { get; set; }
77+
78+
public void Verify(int[] result) {
79+
Debug.Assert(Enumerable.SequenceEqual(result, Expected), $"Test {Id} failed.");
80+
}
81+
}
82+
}

LeetcodeNew.sln

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q20_Valid_Parentheses", "Q2
8181
EndProject
8282
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q242_Valid_Anagram", "Q242_Valid_Anagram\Q242_Valid_Anagram.csproj", "{58B3F7C4-13CB-459E-BBCF-544782378A81}"
8383
EndProject
84+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Formation", "Formation", "{71C7D88B-6D5D-4E25-A275-869603700831}"
85+
EndProject
86+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "21_Days_Challenge", "21_Days_Challenge", "{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}"
87+
EndProject
88+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_1_Remove_Delete_Middle_Node_Q2095", "Formation\21_Days_Challenge\Day_1_Remove_Delete_Middle_Node_Q2095\Day_1_Remove_Delete_Middle_Node_Q2095.csproj", "{F4D0F4C2-26A6-43AC-BC80-D1762684A605}"
89+
EndProject
90+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_2_Spiral_Traversal_Q54", "Formation\21_Days_Challenge\Day_2_Spiral_Traversal_Q54\Day_2_Spiral_Traversal_Q54.csproj", "{82FE764B-4AA7-4694-B9BC-85119629AF56}"
91+
EndProject
8492
Global
8593
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8694
Debug|Any CPU = Debug|Any CPU
@@ -562,5 +570,34 @@ Global
562570
{58B3F7C4-13CB-459E-BBCF-544782378A81}.Release|x64.Build.0 = Release|Any CPU
563571
{58B3F7C4-13CB-459E-BBCF-544782378A81}.Release|x86.ActiveCfg = Release|Any CPU
564572
{58B3F7C4-13CB-459E-BBCF-544782378A81}.Release|x86.Build.0 = Release|Any CPU
573+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
574+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Debug|Any CPU.Build.0 = Debug|Any CPU
575+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Debug|x64.ActiveCfg = Debug|Any CPU
576+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Debug|x64.Build.0 = Debug|Any CPU
577+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Debug|x86.ActiveCfg = Debug|Any CPU
578+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Debug|x86.Build.0 = Debug|Any CPU
579+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Release|Any CPU.ActiveCfg = Release|Any CPU
580+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Release|Any CPU.Build.0 = Release|Any CPU
581+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Release|x64.ActiveCfg = Release|Any CPU
582+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Release|x64.Build.0 = Release|Any CPU
583+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Release|x86.ActiveCfg = Release|Any CPU
584+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605}.Release|x86.Build.0 = Release|Any CPU
585+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
586+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Debug|Any CPU.Build.0 = Debug|Any CPU
587+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Debug|x64.ActiveCfg = Debug|Any CPU
588+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Debug|x64.Build.0 = Debug|Any CPU
589+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Debug|x86.ActiveCfg = Debug|Any CPU
590+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Debug|x86.Build.0 = Debug|Any CPU
591+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|Any CPU.ActiveCfg = Release|Any CPU
592+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|Any CPU.Build.0 = Release|Any CPU
593+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|x64.ActiveCfg = Release|Any CPU
594+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|x64.Build.0 = Release|Any CPU
595+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|x86.ActiveCfg = Release|Any CPU
596+
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|x86.Build.0 = Release|Any CPU
597+
EndGlobalSection
598+
GlobalSection(NestedProjects) = preSolution
599+
{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C} = {71C7D88B-6D5D-4E25-A275-869603700831}
600+
{F4D0F4C2-26A6-43AC-BC80-D1762684A605} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
601+
{82FE764B-4AA7-4694-B9BC-85119629AF56} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
565602
EndGlobalSection
566603
EndGlobal

Library/SingleLinkedListNode.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
using System.Diagnostics;
22
using System.Text;
33

4-
[DebuggerDisplay("{Val}")]
5-
public class SingleLinkedListNode {
6-
public int Val { get; set; }
7-
public SingleLinkedListNode? Next { get; set; } = null;
8-
9-
public SingleLinkedListNode() {
10-
Val = 0;
11-
Next = null;
12-
}
13-
14-
public SingleLinkedListNode(int val, SingleLinkedListNode next) {
15-
Val = val;
16-
Next = next;
17-
}
18-
19-
public void Print() {
20-
StringBuilder sb = new StringBuilder();
21-
SingleLinkedListNode? tmp = this;
22-
23-
while (tmp != null) {
24-
sb.Append($"{tmp.Val} -> ");
25-
tmp = tmp.Next;
4+
namespace Library {
5+
6+
[DebuggerDisplay("{Val}")]
7+
public class SingleLinkedListNode {
8+
public int Val { get; set; }
9+
public SingleLinkedListNode? Next { get; set; } = null;
10+
11+
public SingleLinkedListNode() {
12+
Val = 0;
13+
Next = null;
2614
}
27-
28-
if (sb.Length > 0) {
29-
sb.Length = sb.Length - 4;
15+
16+
public SingleLinkedListNode(int val, SingleLinkedListNode next) {
17+
Val = val;
18+
Next = next;
3019
}
3120

32-
Console.WriteLine(sb.ToString());
21+
public void Print() {
22+
StringBuilder sb = new StringBuilder();
23+
SingleLinkedListNode? tmp = this;
24+
25+
while (tmp != null) {
26+
sb.Append($"{tmp.Val} -> ");
27+
tmp = tmp.Next;
28+
}
29+
30+
if (sb.Length > 0) {
31+
sb.Length = sb.Length - 4;
32+
}
33+
34+
Console.WriteLine(sb.ToString());
35+
}
3336
}
3437
}

Q23_Merge_K_Sorted_Lists/Q23_Merge_K_Sorted_Lists.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#nullable disable warnings
2+
using Library;
3+
24
namespace Q23_Merge_K_Sorted_Lists {
35

46
public class Q23_Merge_K_Sorted_Lists {

Run/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
namespace Run {
22
class Program {
33
static void Main(string[] args) {
4+
5+
#region Formation
6+
7+
//Formation.DeleteMiddleNode.Run(); // Day 1
8+
Formation.SpiralTraversal.Run(); // Day 2
9+
10+
#endregion Formation
11+
12+
#region Misc
413
//Q_Missing_Plus_Two.Q_Missing_Plus_Two.Run();
14+
#endregion Misc
515

616
// Q10_Regular_Expression_Matching.Q10_Regular_Expression_Matching.Run();
717
// Q20_Valid_Parentheses.Q20_Valid_Parentheses.Run();
@@ -17,7 +27,7 @@ static void Main(string[] args) {
1727
// Q211_Design_Add_Search_Words_Data_Structure.Q211_Design_Add_Search_Words_Data_Structure.Run();
1828
// Q215_Kth_Largest_Element.Q215_Kth_Largest_Element.Run();
1929
// Q236_Lowest_Common_Ancestor_of_a_Binary_Tree.Q236_Lowest_Common_Ancestor_of_a_Binary_Tree.Run();
20-
Q242_Valid_Anagram.Q242_Valid_Anagram.Run();
30+
// Q242_Valid_Anagram.Q242_Valid_Anagram.Run();
2131
// Q253_Meeting_Room_II.Q253_Meeting_Room_II.Run();
2232
// Q269_Alien_Dictionary.Q269_Alien_Dictionary.Run();
2333
// Q273_Integer_To_English_Words.Q273_Integer_To_English_Words.Run();

Run/Run.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<ProjectReference Include="..\Q_Missing_Plus_Two\Q_Missing_Plus_Two.csproj" />
4141
<ProjectReference Include="..\Q20_Valid_Parentheses\Q20_Valid_Parentheses.csproj" />
4242
<ProjectReference Include="..\Q242_Valid_Anagram\Q242_Valid_Anagram.csproj" />
43+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_1_Remove_Delete_Middle_Node_Q2095\Day_1_Remove_Delete_Middle_Node_Q2095.csproj" />
44+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_2_Spiral_Traversal_Q54\Day_2_Spiral_Traversal_Q54.csproj" />
4345
</ItemGroup>
4446

4547
<PropertyGroup>

0 commit comments

Comments
 (0)