Skip to content

Commit 74eaa4c

Browse files
paramagparamag
authored andcommitted
Uploading the latest changes with many problems covering dynamic programming, google questions, string hard problems, tries.
1 parent fcfef46 commit 74eaa4c

35 files changed

+997
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@
4848
/Algorithm/Queues/obj/Debug
4949
/Algorithm/Tries/bin/Debug
5050
/Algorithm/Tries/obj/Debug
51+
/Algorithm/.vs/Algorithms/v15

Algorithm/Arrays/MinCoinChange.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,40 @@ namespace Algorithms.Problem.Arrays
1010
[TestClass]
1111
public class MinCoinChange
1212
{
13+
public int GetMinCoinsForBalance(int amount, int[] coins)
14+
{
15+
// create the dp array that contains the coins for 0...amount.
16+
var dp = new int[amount + 1];
17+
18+
// initialize bigger value.
19+
for(int i=0; i <= amount; i++)
20+
{
21+
dp[i] = amount + 1;
22+
}
23+
24+
dp[0] = 0;
25+
for (int i=0;i<= amount; i++)
26+
{
27+
for (int j=0; j< coins.Length; j++)
28+
{
29+
if (coins[j] <= i)
30+
{
31+
// In this case, take the min of dp[i] which is the current number of coins added so far
32+
// and dp[i - coin[j]] which is the amount added previously plus current coin (1)
33+
// [For instance, if the amount is 9 and if there are coins {1,6} and j=1 then dp[9-6] = dp[3] which will be 3 + 1 (current coind which is 6) = 4].
34+
// In above case, when j=0 which is coin=1 then in first attempt the dp[9] = dp[9-1] + 1 = 9. As described above, the dp[9] will be 4.
35+
dp[i] = Math.Min(dp[i], 1 + dp[i - coins[j]]);
36+
}
37+
else
38+
{
39+
break;
40+
}
41+
}
42+
}
43+
44+
return dp[amount] > amount ? -1 : dp[amount];
45+
}
46+
1347
public int GetCoinsForBalance(int balance, int[] coins)
1448
{
1549
int minValue = Int16.MaxValue;
@@ -34,7 +68,7 @@ public int GetCoinsForBalance(int balance, int[] coins)
3468
[TestMethod]
3569
public void TestMinCoinChange()
3670
{
37-
int result = this.GetCoinsForBalance(19, new []{ 10, 6, 1 });
71+
int result = this.GetMinCoinsForBalance(19, new []{ 1, 6, 10 });
3872

3973
Assert.AreEqual(result, 4);
4074
}

Algorithm/BinaryTree/Algorithms.Problem.BinaryTree.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
</ItemGroup>
4646
<ItemGroup>
4747
<Compile Include="BalanceTreeChecker.cs" />
48+
<Compile Include="BinaryTreeRightView.cs" />
4849
<Compile Include="ConnectNextNode.cs" />
4950
<Compile Include="DistanceBetweenTwoNodes.cs" />
5051
<Compile Include="LevelSpiralOrder.cs" />
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using DataStructures.Libraries.Trees;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Algorithms.Problem.BinaryTree.BinaryTreeView
11+
{
12+
public class BinaryTreeNodeEx
13+
{
14+
public BinaryTreeNode treeNode;
15+
public int position;
16+
}
17+
18+
[TestClass]
19+
public class BinaryTreeRightView
20+
{
21+
public void PrintRightViewOfTree(BinaryTreeNode treeNode)
22+
{
23+
var queueBinaryTree = new Queue<BinaryTreeNode>();
24+
25+
queueBinaryTree.Enqueue(treeNode);
26+
queueBinaryTree.Enqueue(null);
27+
28+
while (queueBinaryTree.Any())
29+
{
30+
BinaryTreeNode node = queueBinaryTree.Dequeue();
31+
32+
if (node == null && queueBinaryTree.Peek() != null)
33+
{
34+
Console.WriteLine(queueBinaryTree.Peek());
35+
queueBinaryTree.Enqueue(null);
36+
}
37+
else
38+
{
39+
if (node.Right != null)
40+
queueBinaryTree.Enqueue(node.Right);
41+
42+
if (node.Left != null)
43+
queueBinaryTree.Enqueue(node.Left);
44+
}
45+
}
46+
}
47+
48+
public void PrintTopViewOfTree(BinaryTreeNodeEx treeNode)
49+
{
50+
Dictionary<int, BinaryTreeNodeEx> dictionary = new Dictionary<int, BinaryTreeNodeEx>();
51+
var queueBinaryTree = new Queue<BinaryTreeNodeEx>();
52+
53+
queueBinaryTree.Enqueue(treeNode);
54+
dictionary.Add(0, treeNode);
55+
56+
while (queueBinaryTree.Any())
57+
{
58+
BinaryTreeNodeEx node = queueBinaryTree.Dequeue();
59+
int leftPosition = node.position - 1;
60+
int rightPosition = node.position + 1;
61+
62+
dictionary.Add(leftPosition, node);
63+
}
64+
}
65+
66+
[TestMethod]
67+
public void ValidateTreeSpiralOrder()
68+
{
69+
var binarySearchTree = new BinarySearchTree();
70+
binarySearchTree.PopulateDefaultBalanceTree();
71+
72+
int[] rightView = { 20, 30, 10, 5, 12, 25, 35 };
73+
74+
var spiralOrder = new BinaryTreeRightView();
75+
76+
spiralOrder.PrintRightViewOfTree(binarySearchTree.Root);
77+
}
78+
}
79+
}

Algorithm/DynamicProgramming/Algorithms.Problem.DynamicProgramming.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@
4444
<Reference Include="System.Xml" />
4545
</ItemGroup>
4646
<ItemGroup>
47+
<Compile Include="ClimbingStairs.cs" />
4748
<Compile Include="KnapSackProblem.cs" />
49+
<Compile Include="MinCoinChange.cs" />
4850
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="WightedJobScheduling.cs" />
4952
</ItemGroup>
5053
<ItemGroup>
5154
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace DynamicProgramming
9+
{
10+
[TestClass]
11+
public class ClimbingStairs
12+
{
13+
/// <summary>
14+
/// Returns the number of ways you can climb the stairs.
15+
/// https://leetcode.com/problems/climbing-stairs/description/
16+
/// </summary>
17+
/// <param name="noOfStairs"></param>
18+
/// <returns></returns>
19+
public int ClimbingStairsMethod(int noOfStairs)
20+
{
21+
int[] dp = new int[noOfStairs + 1];
22+
23+
dp[0] = 1;
24+
dp[1] = 1;
25+
26+
for(int i=2; i<= noOfStairs; i++)
27+
{
28+
/// Each time you either climb 1 or 2 steps.
29+
dp[i] = dp[i - 1] + dp[i - 2];
30+
}
31+
return dp[noOfStairs];
32+
}
33+
34+
[TestMethod]
35+
public void TestMethod1()
36+
{
37+
int noOfWays = this.ClimbingStairsMethod(3);
38+
39+
Assert.AreEqual(noOfWays, 3);
40+
}
41+
}
42+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Algorithms.Problem.Arrays
9+
{
10+
[TestClass]
11+
public class MinCoinChange
12+
{
13+
public int GetMinCoinsForBalance(int amount, int[] coins)
14+
{
15+
// create the dp array that contains the coins for 0...amount.
16+
var dp = new int[amount + 1];
17+
18+
// initialize bigger value.
19+
for(int i=0; i <= amount; i++)
20+
{
21+
dp[i] = amount + 1;
22+
}
23+
24+
dp[0] = 0;
25+
for (int i=0;i<= amount; i++)
26+
{
27+
for (int j=0; j< coins.Length; j++)
28+
{
29+
if (coins[j] <= i)
30+
{
31+
// In this case, take the min of dp[i] which is the current number of coins added so far
32+
// and dp[i - coin[j]] which is the amount added previously plus current coin (1)
33+
// [For instance, if the amount is 9 and if there are coins {1,6} and j=1 then dp[9-6] = dp[3] which will be 3 + 1 (current coind which is 6) = 4].
34+
// In above case, when j=0 which is coin=1 then in first attempt the dp[9] = dp[9-1] + 1 = 9. As described above, the dp[9] will be 4.
35+
dp[i] = Math.Min(dp[i], 1 + dp[i - coins[j]]);
36+
}
37+
else
38+
{
39+
break;
40+
}
41+
}
42+
}
43+
44+
return dp[amount] > amount ? -1 : dp[amount];
45+
}
46+
47+
public int GetCoinsForBalance(int balance, int[] coins)
48+
{
49+
int minValue = Int16.MaxValue;
50+
int coinCounter = 0;
51+
52+
if (balance == 0) return 0;
53+
54+
foreach(int coin in coins)
55+
{
56+
if (balance - coin >= 0)
57+
{
58+
coinCounter = this.GetCoinsForBalance(balance - coin, coins);
59+
if (minValue > coinCounter)
60+
{
61+
minValue = coinCounter;
62+
}
63+
}
64+
}
65+
return minValue + 1;
66+
}
67+
68+
[TestMethod]
69+
public void TestMinCoinChange()
70+
{
71+
int result = this.GetMinCoinsForBalance(19, new []{ 1, 6, 10 });
72+
73+
Assert.AreEqual(result, 4);
74+
}
75+
}
76+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace DynamicProgramming
9+
{
10+
public class Job
11+
{
12+
public char JobName;
13+
public int StartTime;
14+
public int EndTime;
15+
public int Weight;
16+
public int CalculatedWeight;
17+
18+
public Job(char jobName, int startTime, int endTime, int weight)
19+
{
20+
this.JobName = jobName;
21+
this.StartTime = startTime;
22+
this.EndTime = endTime;
23+
this.Weight = weight;
24+
this.CalculatedWeight = weight;
25+
}
26+
}
27+
28+
[TestClass]
29+
public class WightedJobScheduling
30+
{
31+
public int GetMaxJobSchedule(List<Job> jobs)
32+
{
33+
List<char> charList = new List<char>();
34+
jobs.Sort(Comparer<Job>.Create((j1, j2) => j1.EndTime.CompareTo(j2.EndTime)));
35+
36+
for(int i=1; i< jobs.Count; i++)
37+
{
38+
for(int j=0; j< i; j++)
39+
{
40+
if (jobs[i].StartTime >= jobs[j].EndTime)
41+
{
42+
jobs[i].CalculatedWeight = Math.Max(jobs[i].CalculatedWeight, jobs[i].Weight + jobs[j].CalculatedWeight);
43+
}
44+
}
45+
}
46+
47+
var maxProfitJob = jobs.Where((j) => j.CalculatedWeight == jobs.Max<Job>((job) => job.CalculatedWeight)).FirstOrDefault();
48+
49+
return maxProfitJob.CalculatedWeight;
50+
}
51+
52+
[TestMethod]
53+
public void TestMethod()
54+
{
55+
var jobs = new Job[] { new Job('a', 1,4, 3), new Job('b', 2, 6, 5), new Job('c', 4,7,2), new Job('d', 5, 9, 4), new Job('e', 6, 8, 6), new Job('f', 7, 10, 8)};
56+
int maxProfit = this.GetMaxJobSchedule(jobs.ToList());
57+
58+
Assert.AreEqual(maxProfit, 13);
59+
}
60+
}
61+
}

Algorithm/InterviewCake/Algorithms.Problem.InterviewCake.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<ItemGroup>
4747
<Compile Include="AppStockProblem.cs" />
4848
<Compile Include="DirtyTestCode.cs" />
49-
<Compile Include="PlayGround.cs" />
49+
<Compile Include="Permutations.cs" />
5050
<Compile Include="Meeting.cs" />
5151
<Compile Include="Properties\AssemblyInfo.cs" />
5252
<Compile Include="RepeatNumbers.cs" />

Algorithm/InterviewCake/DirtyTestCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ public void TestMethod1()
1818
string subStr = str.Substring(0, str.Length - 1);
1919
}
2020
}
21-
}
21+
}

0 commit comments

Comments
 (0)