Skip to content

Commit

Permalink
Adding more coding problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
paramag authored and paramag committed Sep 27, 2018
1 parent 23cf380 commit 35df6d1
Show file tree
Hide file tree
Showing 24 changed files with 1,061 additions and 26 deletions.
Binary file modified Algorithm/.vs/Algorithms/v14/.suo
Binary file not shown.
2 changes: 2 additions & 0 deletions Algorithm/BinaryTree/Algorithms.Problem.BinaryTree.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
<Compile Include="LowestCommonAncestor.cs" />
<Compile Include="PathRootToLeaf.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RootToLeafSum.cs" />
<Compile Include="SerializeAndDeserialize.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
62 changes: 62 additions & 0 deletions Algorithm/BinaryTree/RootToLeafSum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructures.Libraries.Trees
{
[TestClass]
public class RootToLeafSum
{
public bool FindRootToLeafSumPath(BinaryTreeNode root, int sum, List<int> result)
{
// check if it's leaf node
if (root.Left == null && root.Right == null)
{
bool isTarget = false;
if (sum == root.Value)
{
isTarget = true;
result.Add(root.Value);
}

return isTarget;
}

bool isTargetSumLeft = this.FindRootToLeafSumPath(root.Left, sum - root.Value, result);
bool isTargetSumRight = this.FindRootToLeafSumPath(root.Right, sum - root.Value, result);

if (isTargetSumLeft) result.Add(root.Value);
if (isTargetSumRight) result.Add(root.Value);

return isTargetSumLeft || isTargetSumRight;
}

[TestMethod]
public void TestFindRootToLeaftSumPath()
{
/// 20
/// / \
/// 10 30
/// / \ / \
/// 5 12 25 35

var root = new BinaryTreeNode(20);

root.Left = new BinaryTreeNode(10);
root.Right = new BinaryTreeNode(30);

root.Left.Left = new BinaryTreeNode(5);
root.Left.Right = new BinaryTreeNode(12);

root.Right.Left = new BinaryTreeNode(25);
root.Right.Right = new BinaryTreeNode(35);

List<int> listArray = new List<int>();

bool isTargetExist = this.FindRootToLeafSumPath(root, 80, listArray);
}
}
}
77 changes: 77 additions & 0 deletions Algorithm/BinaryTree/SerializeAndDeserialize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using DataStructures.Libraries.Trees;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Problem.BinaryTree
{
[TestClass]
public class SerializeAndDeserialize
{
public BinaryTreeNode DeSerialize(int[] serializedString, int count)
{
// this is leaf node
if (serializedString[count] == -1)
{
return null;
}

int value = serializedString[count];
BinaryTreeNode treeNode = new BinaryTreeNode(value);


treeNode.Left = this.DeSerialize(serializedString, count + 1);
treeNode.Right = this.DeSerialize(serializedString, count + 1);

return treeNode;
}

public string Serialize(BinaryTreeNode root, string str)
{
if (root != null)
{
str += root.Value + ",";

if (root.Left == null && root.Right == null)
{
str = str + "-1,";
System.Diagnostics.Debug.WriteLine(str);

return str;
}
}

if (root != null) str = Serialize(root.Left, str);
if (root!= null) str = Serialize(root.Right, str);

return str;
}

[TestMethod]
public void TestSerialize()
{
var binarySearchTree = new BinarySearchTree();
binarySearchTree.PopulateDefaultBalanceTree();

//BinaryTreeNode node1 = new BinaryTreeNode(10);
//node1.Left = new BinaryTreeNode(5);
//node1.Right = new BinaryTreeNode(12);

string str = this.Serialize(binarySearchTree.Root, "");
string serializedString = str.TrimEnd(',');

string[] serializedStringArray = serializedString.Split(',');
List<int> serializedIntArray = new List<int>();

foreach(string ser in serializedStringArray)
{
serializedIntArray.Add(int.Parse(ser));
}

BinaryTreeNode node = this.DeSerialize(serializedIntArray.ToArray(), 0);
}
}
}
25 changes: 24 additions & 1 deletion Algorithm/LeetCode/Algorithms.Problems.LeetCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,29 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Arrays\AddTwoNumbersProblem.cs" />
<Compile Include="Arrays\ArrayManipulation.cs" />
<Compile Include="Arrays\CombinationSum.cs" />
<Compile Include="Arrays\KMostFrequentElement.cs" />
<Compile Include="Arrays\LengthOfLongestSubStringProblem.cs" />
<Compile Include="Arrays\MedianOfArray.cs" />
<Compile Include="Arrays\MergeTwoSortedArray.cs" />
<Compile Include="Arrays\SearchInArrays.cs" />
<Compile Include="Arrays\SlidingWindowArray.cs" />
<Compile Include="Arrays\SmallestSubArraySum.cs" />
<Compile Include="Arrays\StringBackTracking.cs" />
<Compile Include="Arrays\SubArray.cs" />
<Compile Include="Arrays\TwoSumProblem.cs" />
<Compile Include="BackTrack\BackTrackProblems.cs" />
<Compile Include="BackTrack\BoggleGame.cs" />
<Compile Include="BackTrack\NQueenProblem.cs" />
<Compile Include="BackTrack\TrieDataStructure.cs" />
<Compile Include="Interviews\AmazonPhoneScreening.cs" />
<Compile Include="LinkedList\ListNode.cs" />
<Compile Include="LinkedList\MergeTwoLists.cs" />
<Compile Include="LinkedList\RemoveNthNodeFromList.cs" />
<Compile Include="Matrix\MatrixArraySearch.cs" />
<Compile Include="Matrix\MatrixProblemsBFS.cs" />
<Compile Include="Matrix\MaximumNumberOfSquares.cs" />
<Compile Include="Matrix\RotationMatrix.cs" />
<Compile Include="Matrix\WordSearchProblem.cs" />
<Compile Include="MeetingRoom\Meeting.cs" />
Expand All @@ -72,7 +83,19 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Trees\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\DataStructures\Trees\DataStructures.Libraries.Trees.csproj">
<Project>{845e0ca8-ac51-4bae-95d7-60680cb4a44b}</Project>
<Name>DataStructures.Libraries.Trees</Name>
</ProjectReference>
<ProjectReference Include="..\BinaryTree\Algorithms.Problem.BinaryTree.csproj">
<Project>{4a8c3be7-a268-451a-9b8f-234ed3789866}</Project>
<Name>Algorithms.Problem.BinaryTree</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
70 changes: 70 additions & 0 deletions Algorithm/LeetCode/Arrays/ArrayManipulation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCode.Arrays
{
[TestClass]
public class ArrayManipulation
{
public int[] MoveZerosToRight2(int[] array)
{
int i = 0;
int j = 0;

while (i < array.Length)
{
if (array[i] != 0)
{
if (i != j)
array[j] = array[i];

j++;
}
i++;
}

while (j < array.Length)
{
array[j] = 0;
j++;
}

return array;
}

public int[] MoveZerosToRight(int[] array)
{
for(int i=0; i< array.Length; i++)
{
if (array[i] == 0)
{
int j = i + 1;
while (j < array.Length && array[j] == 0)
{
j++;
}

if (j >= array.Length) break;

// swap the current array
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

return array;
}

[TestMethod]
public void TestMoveZerosToRight()
{
int[] resultArray = this.MoveZerosToRight(new[] { 0, 1, 2, 0, 3, 0 });
resultArray = this.MoveZerosToRight2(new[] { 0, 1,2,0,3,0});
}
}
}
16 changes: 16 additions & 0 deletions Algorithm/LeetCode/Arrays/KMostFrequentElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCode.Arrays
{
public class KMostFrequentElement
{
public int[] GetFrequentElements(int[] array, int k)
{
return null;
}
}
}
48 changes: 48 additions & 0 deletions Algorithm/LeetCode/Arrays/MergeTwoSortedArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCode.Arrays
{
[TestClass]
public class MergeTwoSortedArray
{
public void MergeTwoSortedArrayHelper(int[] arrayA, int[] arrayB)
{
int m = arrayA.Length - 1;
int n = arrayB.Length - 1;

int counterMidA = arrayA.Length - arrayB.Length - 1;
int counterEndA = m;
int counterB = n;

while (counterB >= 0 && counterMidA >= 0)
{
if (arrayA[counterMidA] > arrayB[counterB])
{
arrayA[counterEndA] = arrayA[counterMidA];
counterMidA--;
}
else
{
arrayA[counterEndA] = arrayB[counterB];
counterB--;
}

counterEndA--;
}
}

[TestMethod]
public void TestMergeTwoSortefArray()
{
int[] arrayA = new int[] { 1, 5, 9, 10, 15, 20, 0, 0, 0, 0 };
int[] arrayB = new int[] { 2, 3, 8, 13};

this.MergeTwoSortedArrayHelper(arrayA, arrayB);
}
}
}
Loading

0 comments on commit 35df6d1

Please sign in to comment.