-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graph and stack implementation updates.
- Loading branch information
paramag
authored and
paramag
committed
Aug 29, 2018
1 parent
bfcfa10
commit 6cd4865
Showing
15 changed files
with
694 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Algorithms.Problem.InterviewCake | ||
{ | ||
[TestClass] | ||
public class DirtyTestCode | ||
{ | ||
[TestMethod] | ||
public void TestMethod1() | ||
{ | ||
string str = "cats"; | ||
|
||
string subStr = str.Substring(0, str.Length - 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Algorithms.Problem.InterviewCake | ||
{ | ||
public class RepeatNumbersBeastMode | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
Algorithm/Stacks/GetMiddleElementConstantTimeProblem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Algorithms.Problem.Stacks | ||
{ | ||
public class Node | ||
{ | ||
public int value; | ||
public Node previous; | ||
public Node next; | ||
|
||
public Node(int value) | ||
{ | ||
this.value = value; | ||
} | ||
} | ||
|
||
[TestClass] | ||
public class GetMiddleElementConstantTimeProblem | ||
{ | ||
private Node doubleLinkedList; | ||
private Node middleElement; | ||
public int Counter = 0; | ||
|
||
public void Push(int value) | ||
{ | ||
Node node = new Node(value); | ||
|
||
if (doubleLinkedList == null) | ||
{ | ||
doubleLinkedList = node; | ||
middleElement = doubleLinkedList; | ||
} | ||
else | ||
{ | ||
doubleLinkedList.next = node; | ||
doubleLinkedList.next.previous = doubleLinkedList; | ||
|
||
doubleLinkedList = doubleLinkedList.next; | ||
} | ||
|
||
this.Counter += 1; | ||
|
||
if (this.Counter > 1 && !(this.Counter % 2 == 0)) | ||
{ | ||
middleElement = middleElement.next; | ||
} | ||
} | ||
|
||
public int Pop() | ||
{ | ||
int value = doubleLinkedList.value; | ||
doubleLinkedList = doubleLinkedList.previous; | ||
doubleLinkedList.next = null; | ||
|
||
this.Counter -= 1; | ||
|
||
if (!(this.Counter % 2 == 0)) | ||
{ | ||
middleElement = middleElement.previous; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
public int GetMiddleElement() | ||
{ | ||
return middleElement.value; | ||
} | ||
|
||
[TestMethod] | ||
public void TestMiddleElementConstantTime() | ||
{ | ||
this.Push(1); | ||
this.Push(2); | ||
this.Push(3); | ||
|
||
Assert.AreEqual(2, GetMiddleElement()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMiddleElementConstantTime2() | ||
{ | ||
this.Push(1); | ||
this.Push(2); | ||
this.Push(3); | ||
this.Push(4); | ||
|
||
Assert.AreEqual(2, GetMiddleElement()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMiddleElementConstantTime3() | ||
{ | ||
this.Push(1); | ||
this.Push(2); | ||
this.Push(3); | ||
this.Push(4); | ||
this.Push(5); | ||
|
||
Assert.AreEqual(3, GetMiddleElement()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMiddleElementConstantTime4() | ||
{ | ||
this.Push(1); | ||
this.Push(2); | ||
this.Push(3); | ||
this.Push(4); | ||
this.Push(5); | ||
this.Pop(); | ||
|
||
Assert.AreEqual(3, GetMiddleElement()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMiddleElementConstantTime5() | ||
{ | ||
this.Push(1); | ||
this.Push(2); | ||
this.Push(3); | ||
this.Push(4); | ||
this.Push(5); | ||
|
||
this.Pop(); | ||
this.Pop(); | ||
|
||
Assert.AreEqual(2, GetMiddleElement()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,71 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Stacks | ||
{ | ||
/// <summary> | ||
/// Get minimum value from stack in constant time. | ||
/// </summary> | ||
[TestClass] | ||
public class MinValueInConstantTime | ||
{ | ||
private Stack<int> stack = new Stack<int>(); | ||
private Stack<int> auxillaryStack = new Stack<int>(); | ||
|
||
public int GetMinimumValue() | ||
{ | ||
return this.auxillaryStack.Peek(); | ||
} | ||
|
||
public void Push(int value) | ||
{ | ||
this.stack.Push(value); | ||
|
||
// Use the aux stack (2nd) to push the value if the top of the aux stack is less than the value. | ||
// Also push to aux stack if the stack is empty. | ||
if (this.auxillaryStack.Count == 0 || this.auxillaryStack.Peek() > value) | ||
{ | ||
this.auxillaryStack.Push(value); | ||
} | ||
else | ||
{ | ||
int auxValue = this.auxillaryStack.Peek(); | ||
this.auxillaryStack.Push(auxValue); | ||
} | ||
} | ||
|
||
public int Pop() | ||
{ | ||
int value = this.stack.Pop(); | ||
|
||
// remove from the aux stack if the value is equal to the top of the stack. | ||
if (this.auxillaryStack.Peek() == value) | ||
{ | ||
this.auxillaryStack.Pop(); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetMinimumInConstantTime() | ||
{ | ||
this.Push(10); | ||
this.Push(8); | ||
this.Push(11); | ||
this.Push(5); | ||
this.Push(2); | ||
this.Push(20); | ||
this.Push(17); | ||
this.Push(1); | ||
|
||
int value = this.GetMinimumValue(); | ||
|
||
Assert.AreEqual(value, 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.