Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Ch8 Q1, show elapsed time #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "ctci.sln"
}
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
8 changes: 4 additions & 4 deletions Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +71,7 @@ public override void Run()

AssortedMethods.PrintMatrix(matrix);

Rotate(matrix, size);
Rotate(matrix);
Console.WriteLine();
AssortedMethods.PrintMatrix(matrix);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\ctci.Contracts\ctci.Contracts.csproj" />
<ProjectReference Include="..\ctci.Library\ctci.Library.csproj" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RootNamespace>Chapter08</RootNamespace>
<!-- <ImplicitUsings></ImplicitUsings>
<Nullable>enable</Nullable> -->
</PropertyGroup>

</Project>
69 changes: 69 additions & 0 deletions Ch 08. Recursion and Dynamic Programming/Q8_01_Triple_Step.cs
Original file line number Diff line number Diff line change
@@ -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<int>(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}");
}
}
}
}
8 changes: 8 additions & 0 deletions ctci.Library/AssortedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,5 +2306,13 @@ public static string[] GetListOfWords()
};
return wordList;
}

public static void FillArray<T>(T[] arr, T val)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i] = val;
}
}
}
}
7 changes: 7 additions & 0 deletions ctci.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
38 changes: 25 additions & 13 deletions ctci/Program.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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(),
},
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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}");
}
}

Expand Down
1 change: 1 addition & 0 deletions ctci/ctci.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ProjectReference Include="..\Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.csproj" />
<ProjectReference Include="..\Ch 16. Moderate\Ch 16. Moderate.csproj" />
<ProjectReference Include="..\Introduction\Introduction.csproj" />
<ProjectReference Include="..\Ch 08. Recursion and Dynamic Programming\Ch 08. Recursion and Dynamic Programming.csproj" />
</ItemGroup>

</Project>