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

3.2 Stack Min #15

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Ch 03. Stacks and Queues/3.2 Stack Min.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using ctci.Contracts;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3._2_Stack_Min
{
class Q3_02_StackMin : Question
{
public override void Run()
{
//LD create and populate a stack
StackOverride aStack = new StackOverride();

aStack.Push(6);
aStack.Push(9);
aStack.Push(3);
aStack.Push(12);
Console.WriteLine("Min expected 3 -> " + aStack.getCurrentMin());
aStack.Pop();
aStack.Pop();
Console.WriteLine("Min expected 6 -> " + aStack.getCurrentMin());
aStack.Push(4);
Console.WriteLine("Min expected 4 -> " + aStack.getCurrentMin());
aStack.Push(4);
aStack.Push(44);
Console.WriteLine("Min expected 4 -> " + aStack.getCurrentMin());

Console.ReadLine();
}

#region Support Class
// Override Stack class, add additional "supportStack" to keep track of mins.
// push in supportStack new min when pushing value in main stack <= supportStack current min
// pop from support stack when popping value from main stack equal current supportStack min

public class StackOverride : Stack
{
Stack<int> supportStack;

public StackOverride() : base()
{
//LD adding a support stack
supportStack = new Stack<int>();
}

public int getCurrentMin()
{
if (supportStack.Count() == 0)
{
return int.MaxValue;
}
else
{
return supportStack.Peek();
}
}

// redefine "Push"
public void Push(int value)
{
if (value <= getCurrentMin())
{
supportStack.Push(value);
}

base.Push(value);
}

// redefine "Pop"
public int Pop()
{
int value = (int)base.Pop();
if (value == getCurrentMin())
{
supportStack.Pop();
}

return value;
}
}

#endregion Support Class End

}//class
}//namespace
12 changes: 12 additions & 0 deletions Ch 03. Stacks and Queues/Ch 03. Stacks and Queues.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

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

</Project>
30 changes: 20 additions & 10 deletions ctci.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ctci", "ctci\ctci.csproj", "{F7867FE4-E80B-44DD-9666-C82E6D401B8A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ctci", "ctci\ctci.csproj", "{F7867FE4-E80B-44DD-9666-C82E6D401B8A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 01. Arrays and Strings", "Ch 01. Arrays and Strings\Ch 01. Arrays and Strings.csproj", "{3F2DF0FA-D7A0-479B-B720-3AB7411E9539}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 01. Arrays and Strings", "Ch 01. Arrays and Strings\Ch 01. Arrays and Strings.csproj", "{3F2DF0FA-D7A0-479B-B720-3AB7411E9539}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ctci.Contracts", "ctci.Contracts\ctci.Contracts.csproj", "{D1B538B9-DE81-4FFF-93B2-A958ED2CE4DA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ctci.Contracts", "ctci.Contracts\ctci.Contracts.csproj", "{D1B538B9-DE81-4FFF-93B2-A958ED2CE4DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ctci.Library", "ctci.Library\ctci.Library.csproj", "{8DC982E1-042C-4652-BBC4-CD375223C852}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ctci.Library", "ctci.Library\ctci.Library.csproj", "{8DC982E1-042C-4652-BBC4-CD375223C852}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Introduction", "Introduction\Introduction.csproj", "{AF745A68-20A9-4FF4-801C-86A3B0383C49}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Introduction", "Introduction\Introduction.csproj", "{AF745A68-20A9-4FF4-801C-86A3B0383C49}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Helpers", "Helpers", "{F9BD47E4-94CA-4823-B074-22E8ABE9B8C8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Questions", "Questions", "{8E563D72-A200-4BB7-8AB8-A93BEED3A5B7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 05. Bit Manipulation", "Ch 05. Bit Manipulation\Ch 05. Bit Manipulation.csproj", "{6515F74F-66C6-414D-B5EC-10260AAD473C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 05. Bit Manipulation", "Ch 05. Bit Manipulation\Ch 05. Bit Manipulation.csproj", "{6515F74F-66C6-414D-B5EC-10260AAD473C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 02. Linked Lists", "Ch 02. Linked Lists\Ch 02. Linked Lists.csproj", "{D1ACE913-9486-4CDC-B980-A5F848A67108}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 02. Linked Lists", "Ch 02. Linked Lists\Ch 02. Linked Lists.csproj", "{D1ACE913-9486-4CDC-B980-A5F848A67108}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 10. Sorting and Searching", "Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.csproj", "{90D56A57-CEAD-42F8-A978-BC2D33530E0E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 10. Sorting and Searching", "Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.csproj", "{90D56A57-CEAD-42F8-A978-BC2D33530E0E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 16. Moderate", "Ch 16. Moderate\Ch 16. Moderate.csproj", "{AE0D044B-1AE9-430F-B05D-ADAC72C407B8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 16. Moderate", "Ch 16. Moderate\Ch 16. Moderate.csproj", "{AE0D044B-1AE9-430F-B05D-ADAC72C407B8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 04. Trees", "Ch 04. Trees\Ch 04. Trees.csproj", "{AF583937-1A80-407F-B683-3FEED7F3BC16}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch 04. Trees", "Ch 04. Trees\Ch 04. Trees.csproj", "{AF583937-1A80-407F-B683-3FEED7F3BC16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch 03. Stacks and Queues", "Ch 03. Stacks and Queues\Ch 03. Stacks and Queues.csproj", "{1BAACA9C-6A2B-43D3-AD35-3DC00F8F570C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
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
{1BAACA9C-6A2B-43D3-AD35-3DC00F8F570C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1BAACA9C-6A2B-43D3-AD35-3DC00F8F570C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BAACA9C-6A2B-43D3-AD35-3DC00F8F570C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BAACA9C-6A2B-43D3-AD35-3DC00F8F570C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -88,5 +94,9 @@ 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}
{1BAACA9C-6A2B-43D3-AD35-3DC00F8F570C} = {8E563D72-A200-4BB7-8AB8-A93BEED3A5B7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {15F14EAA-AAD6-4406-928A-3AAC4FA56322}
EndGlobalSection
EndGlobal