From 7901913412faa1e9386ce0d30246f3dc42eb65d5 Mon Sep 17 00:00:00 2001 From: shea-c4 Date: Thu, 17 Aug 2023 04:40:31 -0400 Subject: [PATCH] add Ch8 Q1, show elapsed time --- .vscode/launch.json | 24 +++++++ .vscode/settings.json | 3 + .vscode/tasks.json | 41 +++++++++++ .../Q1_07_Rotate_Matrix.cs | 8 +-- .... Recursion and Dynamic Programming.csproj | 15 ++++ .../Q8_01_Triple_Step.cs | 69 +++++++++++++++++++ ctci.Library/AssortedMethods.cs | 8 +++ ctci.sln | 7 ++ ctci/Program.cs | 38 ++++++---- ctci/ctci.csproj | 1 + 10 files changed, 197 insertions(+), 17 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 Ch 08. Recursion and Dynamic Programming/Ch 08. Recursion and Dynamic Programming.csproj create mode 100644 Ch 08. Recursion and Dynamic Programming/Q8_01_Triple_Step.cs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ccad7f7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/ctci/bin/Debug/netcoreapp1.1/ctci.dll", + "args": [], + "cwd": "${workspaceFolder}/ctci", + "console": "integratedTerminal", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6a105b3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "ctci.sln" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6b872cf --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/ctci.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/ctci.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/ctci.sln" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs b/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs index a6bc5c4..2026d4c 100644 --- a/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs +++ b/Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs @@ -31,10 +31,10 @@ private void Rotate(int[][] matrix) { if (!IsSquareMatrix(matrix)) // Edge case + error checking. { - return false; + return; } - - int matrixSize = matrix.Length + + int matrixSize = matrix.Length; for (int layer = 0; layer < matrixSize / 2; layer++) { int startIndex = layer; @@ -71,7 +71,7 @@ public override void Run() AssortedMethods.PrintMatrix(matrix); - Rotate(matrix, size); + Rotate(matrix); Console.WriteLine(); AssortedMethods.PrintMatrix(matrix); } diff --git a/Ch 08. Recursion and Dynamic Programming/Ch 08. Recursion and Dynamic Programming.csproj b/Ch 08. Recursion and Dynamic Programming/Ch 08. Recursion and Dynamic Programming.csproj new file mode 100644 index 0000000..7cdbb75 --- /dev/null +++ b/Ch 08. Recursion and Dynamic Programming/Ch 08. Recursion and Dynamic Programming.csproj @@ -0,0 +1,15 @@ + + + + + + + + + netcoreapp1.1 + Chapter08 + + + + diff --git a/Ch 08. Recursion and Dynamic Programming/Q8_01_Triple_Step.cs b/Ch 08. Recursion and Dynamic Programming/Q8_01_Triple_Step.cs new file mode 100644 index 0000000..de80aed --- /dev/null +++ b/Ch 08. Recursion and Dynamic Programming/Q8_01_Triple_Step.cs @@ -0,0 +1,69 @@ +using ctci.Contracts; +using ctci.Library; +using System; + +namespace Chapter08 +{ + public class Q8_01_Triple_Step : Question + { + public class QuestionA + { + public static int CountWays(int n) + { + if (n < 0) + { + return 0; + } + else if (n == 0) + { + return 1; + } + else + { + return CountWays(n - 1) + CountWays(n - 2) + CountWays(n - 3); + } + } + } + + public class QuestionB + { + public static int CountWays(int n) + { + int[] map = new int[n + 1]; + AssortedMethods.FillArray(map, -1); + return CountWays(n, map); + } + + public static int CountWays(int n, int[] memo) + { + if (n < 0) + { + return 0; + } + else if (n == 0) + { + return 1; + } + else if (memo[n] > -1) + { + return memo[n]; + } + else + { + memo[n] = CountWays(n - 1, memo) + CountWays(n - 2, memo) + CountWays(n - 3, memo); + return memo[n]; + } + } + } + + public override void Run() + { + for (int i = 0; i < 30; i++) + { + int c1 = QuestionB.CountWays(i); + int c2 = QuestionA.CountWays(i); + Console.WriteLine($"{i}: {c1} {c2}"); + } + } + } +} diff --git a/ctci.Library/AssortedMethods.cs b/ctci.Library/AssortedMethods.cs index f0e661f..f389d94 100644 --- a/ctci.Library/AssortedMethods.cs +++ b/ctci.Library/AssortedMethods.cs @@ -2306,5 +2306,13 @@ public static string[] GetListOfWords() }; return wordList; } + + public static void FillArray(T[] arr, T val) + { + for (int i = 0; i < arr.Length; i++) + { + arr[i] = val; + } + } } } \ No newline at end of file diff --git a/ctci.sln b/ctci.sln index a5422bd..e003bec 100644 --- a/ctci.sln +++ b/ctci.sln @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 16. Moderate", "Ch 16. M EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 04. Trees", "Ch 04. Trees\Ch 04. Trees.csproj", "{AF583937-1A80-407F-B683-3FEED7F3BC16}" EndProject +Project("{8E563D72-A200-4BB7-8AB8-A93BEED3A5B7}") = "Ch 08. Recursion and Dynamic Programming", "Ch 08. Recursion and Dynamic Programming\Ch 08. Recursion and Dynamic Programming.csproj", "{CF860E19-E678-4DB8-BF4A-87A92B58F7E7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -73,6 +75,10 @@ Global {AF583937-1A80-407F-B683-3FEED7F3BC16}.Debug|Any CPU.Build.0 = Debug|Any CPU {AF583937-1A80-407F-B683-3FEED7F3BC16}.Release|Any CPU.ActiveCfg = Release|Any CPU {AF583937-1A80-407F-B683-3FEED7F3BC16}.Release|Any CPU.Build.0 = Release|Any CPU + {CF860E19-E678-4DB8-BF4A-87A92B58F7E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CF860E19-E678-4DB8-BF4A-87A92B58F7E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CF860E19-E678-4DB8-BF4A-87A92B58F7E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CF860E19-E678-4DB8-BF4A-87A92B58F7E7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -88,5 +94,6 @@ Global {90D56A57-CEAD-42F8-A978-BC2D33530E0E} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} {AE0D044B-1AE9-430F-B05D-ADAC72C407B8} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} {AF583937-1A80-407F-B683-3FEED7F3BC16} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} + {CF860E19-E678-4DB8-BF4A-87A92B58F7E7} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7} EndGlobalSection EndGlobal diff --git a/ctci/Program.cs b/ctci/Program.cs index eb053fb..485c518 100644 --- a/ctci/Program.cs +++ b/ctci/Program.cs @@ -1,12 +1,14 @@ using Chapter01; using Chapter02; -using Chapter04; +//using Chapter04; using Chapter05; +using Chapter08; using Chapter10; using Chapter16; using ctci.Contracts; using Introduction; using System; +using System.Diagnostics; namespace ctci { @@ -28,7 +30,7 @@ private static void Main(string[] args) new Q1_04_Palindrome_Permutation(), new Q1_05_One_Away_A(), new Q1_06_String_Compression(), - new Q1_07_Rotate_Matrix(), + // new Q1_07_Rotate_Matrix(), new Q1_08_Zero_Matrix(), new Q1_09_String_Rotation(), }, @@ -44,17 +46,17 @@ private static void Main(string[] args) new Q2_08_Loop_Detection() }, - new Question[] { - new Q4_02_CreateMinimalBSTfromSortedUniqueArray(), - new Q4_03_List_of_Depths(), - new Q4_04_CheckBalanced(), - new Q4_05_Validate_BST(), - new Q4_06_Successor(), - new Q4_08_LowestCommonAncestorNotBST(), - new Q4_09_BST_Sequence(), - new Q4_10_Check_SubTree(), - new ReplaceNodeInImmutableTree() - }, + // new Question[] { + // new Q4_02_CreateMinimalBSTfromSortedUniqueArray(), + // new Q4_03_List_of_Depths(), + // new Q4_04_CheckBalanced(), + // new Q4_05_Validate_BST(), + // new Q4_06_Successor(), + // new Q4_08_LowestCommonAncestorNotBST(), + // new Q4_09_BST_Sequence(), + // new Q4_10_Check_SubTree(), + // new ReplaceNodeInImmutableTree() + // }, new Question[] { new Q5_01_Insertion(), @@ -66,6 +68,10 @@ private static void Main(string[] args) new Q5_08_Draw_Line() }, + new Question[] { + new Q8_01_Triple_Step() + }, + new Question[] { new Q10_01_Sorted_Merge(), new Q10_02_Group_Anagrams(), @@ -92,13 +98,19 @@ private static void Main(string[] args) foreach (var chapter in chapters) { + Stopwatch stopwatch = new Stopwatch(); foreach (Question q in chapter) { Console.WriteLine("\n\n"); Console.WriteLine($"// Executing: {q.Name}"); Console.WriteLine("// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----"); + stopwatch.Start(); q.Run(); + stopwatch.Stop(); + + Console.WriteLine(); + Console.WriteLine($"// Elapsed: {stopwatch.Elapsed}"); } } diff --git a/ctci/ctci.csproj b/ctci/ctci.csproj index 20fcae9..0b8a747 100644 --- a/ctci/ctci.csproj +++ b/ctci/ctci.csproj @@ -17,6 +17,7 @@ + \ No newline at end of file