-
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.
- Loading branch information
paramag
authored and
paramag
committed
Sep 17, 2018
1 parent
f03c89a
commit 23cf380
Showing
9 changed files
with
378 additions
and
4 deletions.
There are no files selected for viewing
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
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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.