Skip to content

Commit

Permalink
practice
Browse files Browse the repository at this point in the history
  • Loading branch information
paramag authored and paramag committed Sep 17, 2018
1 parent f03c89a commit 23cf380
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 4 deletions.
Binary file modified Algorithm/.vs/Algorithms/v14/.suo
Binary file not shown.
18 changes: 18 additions & 0 deletions Algorithm/Algorithms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms.Problem.Intervie
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStructures.Libraries.Graph", "..\DataStructures\Graph\DataStructures.Libraries.Graph.csproj", "{6EB3565B-2658-42A7-80CB-223B6E42013A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1\ConsoleApplication1.csproj", "{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -197,6 +199,22 @@ Global
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x64.Build.0 = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x86.ActiveCfg = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x86.Build.0 = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|ARM.ActiveCfg = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|ARM.Build.0 = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|x64.ActiveCfg = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|x64.Build.0 = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|x86.ActiveCfg = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Debug|x86.Build.0 = Debug|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|Any CPU.Build.0 = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|ARM.ActiveCfg = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|ARM.Build.0 = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|x64.ActiveCfg = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|x64.Build.0 = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|x86.ActiveCfg = Release|Any CPU
{018D6E54-5FE2-45E3-97FB-94FFD5F3DF6C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions Algorithm/LeetCode/Algorithms.Problems.LeetCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@
<Compile Include="Arrays\LengthOfLongestSubStringProblem.cs" />
<Compile Include="Arrays\MedianOfArray.cs" />
<Compile Include="Arrays\SearchInArrays.cs" />
<Compile Include="Arrays\SubArray.cs" />
<Compile Include="Arrays\TwoSumProblem.cs" />
<Compile Include="BackTrack\BackTrackProblems.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\RotationMatrix.cs" />
<Compile Include="Matrix\WordSearchProblem.cs" />
<Compile Include="MeetingRoom\Meeting.cs" />
<Compile Include="MeetingRoom\MeetingRoomProblems.cs" />
<Compile Include="PlayGround.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="String\EvaluateExpression.cs" />
<Compile Include="String\PhoneLetterCombinationLexicographical.cs" />
<Compile Include="String\LexicographicOrder.cs" />
<Compile Include="String\FastProgrammingStringManipulation.cs" />
<Compile Include="String\ZigZagDisplayCharacters.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Algorithm/LeetCode/Arrays/CombinationSum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public List<int> RangeSum(int[] array, int k)
}

List<int> result_Array = new List<int>{ sum };
for (int i=k; i< array.Length; i++)
for (int i=1; i< array.Length; i++)
{
int intermediateSum = array[i] + result_Array[i - k] - array[i - k];

Expand Down
110 changes: 110 additions & 0 deletions Algorithm/LeetCode/Arrays/SubArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 SubArray
{
public class Result
{
public int startIndex;
public int endIndex;
public int sum_so_far = int.MaxValue;
}

public Result MinimumSubArray(int[] array)
{
Result result = new Result();

int start_index = -1;
int end_index = -1;
int min_sum_SubArray = int.MaxValue;

for(int i=0;i < array.Length; i++)
{
if (min_sum_SubArray < 0 )
{
if (start_index < 0)
{
start_index = i;
}

min_sum_SubArray += array[i];

end_index = i;
}
else
{
start_index = -1;
end_index = -1;
min_sum_SubArray = array[i];
}

// save the previous ones if it's already set.
if (result.sum_so_far > min_sum_SubArray)
{
result.startIndex = start_index;
result.endIndex = end_index;
result.sum_so_far = min_sum_SubArray;
}
}

return result;
}

public List<List<int>> GetArraysInRange(int[] originalArray, int range)
{
List<List<int>> resultArrays = new List<List<int>>();

for(int i=0; i<= originalArray.Length - range; i++)
{
int[] temp = new int[range];
for(int start = 0; start < range; start++)
{
temp[start] = originalArray[start + i];
}

resultArrays.Add(temp.ToList());
}

return resultArrays;
}

public int SumSubArrayMins(int[] A)
{
// answer should be {3}, {1}, {2}, {4}, {3,1}, {1,2}, {2,4}, {3,1,2} {1,2,4} {3,1,2,4}
// min_answer should be {3}, {1}, {2}, {4}, {1}, {1}, {2}, {1}, {1}, {1}
int k = 1;
List<List<int>> resultArrays = new List<List<int>>();
while (k <= A.Length)
{
List<List<int>> temp = this.GetArraysInRange(A, k);

resultArrays.AddRange(temp);
k = k + 1;
}

List<int> minResultArray = new List<int>();
foreach (List<int> array in resultArrays)
{
Result result = this.MinimumSubArray(array.ToArray());
minResultArray.Add(result.sum_so_far);
}

return minResultArray.Sum();
}

[TestMethod]
public void TestMinimumSubArray()
{
int[] arr = { 3, 1, 2, 4 };
int result = this.SumSubArrayMins(arr);
Assert.AreEqual(result, 17);
}
}
}
52 changes: 52 additions & 0 deletions Algorithm/LeetCode/Matrix/MatrixArraySearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCode.Matrix
{
[TestClass]
public class MatrixArraySearch
{
public bool SearchInSortedMatrix(int[,] matrix, int target)
{
int rowMax = matrix.GetLength(0);
int colMax = matrix.GetLength(1);

for (int i = 0, j = colMax - 1; j >= 0 && i < rowMax;)
{
if (target > matrix[i, j])
{
i = i + 1;
}
else if (target < matrix[i, j])
{
j = j - 1;
}
else
{
return true;
}
}

return false;
}

[TestMethod]
public void TestSortedMatrixSearch()
{
int[,] mat = {
{ 10, 20, 30, 40},
{ 15, 25, 35, 45},
{ 27, 29, 37, 48},
{ 32, 33, 39, 50}
};

bool result = this.SearchInSortedMatrix(mat, 10);

Assert.IsTrue(result);
}
}
}
84 changes: 84 additions & 0 deletions Algorithm/LeetCode/String/EvaluateExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCode.String
{
[TestClass]
public class EvaluateExpression
{
private double EvaluateMathExpression(string expression)
{
DataTable table = new DataTable();
table.Columns.Add("expression", typeof(string), expression);

DataRow row = table.NewRow();
table.Rows.Add(row);

return double.Parse((string)row["expression"]);
}

private double GetNumber(string expression, ref int counter)
{
double temp = 0;
string result = string.Empty;

while (counter < expression.Length && double.TryParse(expression[counter].ToString(), out temp))
{
result += expression[counter].ToString();
counter += 1;
}

double result_Integer = -1;
double.TryParse(result.ToString(), out result_Integer);

return result_Integer;

}

public double EvaluateExpressionMethod(string expression)
{
double number1 = 0;
double number2 = 0;

int counter = 0;

number1 = this.GetNumber(expression, ref counter);

while (counter < expression.Length)
{
string _operator = string.Empty;
if (counter + 1 < expression.Length)
{
_operator = expression[counter].ToString();
}

if (counter + 1 < expression.Length)
{
++counter;
number2 = this.GetNumber(expression, ref counter);
}

string expressionBuilder = number1.ToString() + _operator + number2.ToString();

Console.WriteLine("Expression Builder: " + expressionBuilder);

number1 = this.EvaluateMathExpression(expressionBuilder);
}

return number1;

}

[TestMethod]
public void TestMethod()
{
double result = this.EvaluateExpressionMethod("200+300");
Console.WriteLine(result);
}
}
}
Loading

0 comments on commit 23cf380

Please sign in to comment.