Skip to content

Commit c3e2e76

Browse files
Adding descriptions.
1 parent 8dc3eb2 commit c3e2e76

File tree

7 files changed

+212
-18
lines changed

7 files changed

+212
-18
lines changed

AlgorithmnsExercises.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Famous Algorithms", "Famous
1717
EndProject
1818
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DataStructures", "DataStructures", "{E047B6E0-EA10-457F-A029-8E0F126D5DD1}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinkedList", "DataStuctures\LinkedList\LinkedList.csproj", "{24F5D8C8-5E51-40AE-9D9D-293C4D5C745B}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkedList", "DataStuctures\LinkedList\LinkedList.csproj", "{24F5D8C8-5E51-40AE-9D9D-293C4D5C745B}"
2121
EndProject
2222
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Easy_Algorithms", "Algorithms\Easy_Algorithms\Easy_Algorithms.csproj", "{607A12AE-23BF-44E0-A2B8-37BCAF473D65}"
2323
EndProject
@@ -55,6 +55,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnionFind", "Algorithms\Fam
5555
EndProject
5656
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnionFind.Tests", "Algorithms\FamousAlgorithms\UnionFind.Tests\UnionFind.Tests.csproj", "{E9B377DA-68F5-4D14-81A0-0730E1D46F94}"
5757
EndProject
58+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Medium_Algorithms", "Algorithms\Medium_Algorithms\Medium_Algorithms.csproj", "{145C1215-9B40-4568-A0FA-3742BE12574E}"
59+
EndProject
60+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Medium_Algorithms.Tests", "Algorithms\Medium_Algorithms.Tests\Medium_Algorithms.Tests.csproj", "{2BC2240B-D0B5-4D9B-85CD-E94F37C32612}"
61+
EndProject
5862
Global
5963
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6064
Debug|Any CPU = Debug|Any CPU
@@ -137,6 +141,14 @@ Global
137141
{E9B377DA-68F5-4D14-81A0-0730E1D46F94}.Debug|Any CPU.Build.0 = Debug|Any CPU
138142
{E9B377DA-68F5-4D14-81A0-0730E1D46F94}.Release|Any CPU.ActiveCfg = Release|Any CPU
139143
{E9B377DA-68F5-4D14-81A0-0730E1D46F94}.Release|Any CPU.Build.0 = Release|Any CPU
144+
{145C1215-9B40-4568-A0FA-3742BE12574E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
145+
{145C1215-9B40-4568-A0FA-3742BE12574E}.Debug|Any CPU.Build.0 = Debug|Any CPU
146+
{145C1215-9B40-4568-A0FA-3742BE12574E}.Release|Any CPU.ActiveCfg = Release|Any CPU
147+
{145C1215-9B40-4568-A0FA-3742BE12574E}.Release|Any CPU.Build.0 = Release|Any CPU
148+
{2BC2240B-D0B5-4D9B-85CD-E94F37C32612}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
149+
{2BC2240B-D0B5-4D9B-85CD-E94F37C32612}.Debug|Any CPU.Build.0 = Debug|Any CPU
150+
{2BC2240B-D0B5-4D9B-85CD-E94F37C32612}.Release|Any CPU.ActiveCfg = Release|Any CPU
151+
{2BC2240B-D0B5-4D9B-85CD-E94F37C32612}.Release|Any CPU.Build.0 = Release|Any CPU
140152
EndGlobalSection
141153
GlobalSection(SolutionProperties) = preSolution
142154
HideSolutionNode = FALSE
@@ -166,6 +178,8 @@ Global
166178
{A1A9F596-76F9-427F-94DE-A8D2B19847E1} = {D5CCEB3D-5A7B-4C80-A670-3D9737ABDA7C}
167179
{359A92AA-BAB8-409B-BB17-802CB98A5BB0} = {D5CCEB3D-5A7B-4C80-A670-3D9737ABDA7C}
168180
{E9B377DA-68F5-4D14-81A0-0730E1D46F94} = {D5CCEB3D-5A7B-4C80-A670-3D9737ABDA7C}
181+
{145C1215-9B40-4568-A0FA-3742BE12574E} = {B6D795F9-DCD6-4A0E-A2A7-6DE36D63F083}
182+
{2BC2240B-D0B5-4D9B-85CD-E94F37C32612} = {B6D795F9-DCD6-4A0E-A2A7-6DE36D63F083}
169183
EndGlobalSection
170184
GlobalSection(ExtensibilityGlobals) = postSolution
171185
SolutionGuid = {B236DD2A-F3D2-471E-805D-C9952324EAEA}

Algorithms/Easy_Algorithms/EasyAlgorithmsClass.cs

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,26 @@ public bool ClassPhotos(List<int> redShirtHeights, List<int> blueShirtHeights)
580580

581581
#region TandemBicycle
582582
/// <summary>
583-
///
583+
/// A tandem bicycle is a bicycle that's operated by two people: Person A and Person B. Both
584+
/// people pedal bicycle, but the person that pedals faster dictates the speed of the bicycle.
585+
/// So if person A pedals at speed of 5, and person B pedal at a sped of 4, the tandem bicycle
586+
/// moved st a speed of 5(i.e tandemSped = Max(speedA,speedB).
587+
///
588+
/// You're give tow list of positive integer: one that contains the speed of riders is wearing
589+
/// red shirts, and another is wearing blue shirts. Each rider is represented by a single positive
590+
/// integer, which is the speed that the pedal a tandem bicyle at. Both lists have the same length
591+
/// meaning that there are as many red shirt riders as there are blue shirt riders. Your goal os
592+
/// to pair every rider wearing a red shirt with a rider wearing a blue shirt to operate a tandem
593+
/// blue shirt.
594+
///
595+
/// Write a function that returns the maximum possible total speed or the minimum possible total speed
596+
/// of all the tandem bicycle being ridden based on the input parameter, fastest. if fastest = tru.
597+
/// your function should return the maximum possible total speed; otherwise it should return
598+
/// the minimum total speed.
599+
///
600+
/// Total speed is defined as the sum of the speeds of the tandem bicycle being ridden. For example
601+
/// if there are 4 riders who have speeds 1 3 4 5 and if they're paired to tandem bicycle as follows
602+
/// [1,4],[5,3] then the total speed of these tandem bicycle is 4 + 5 = 9
584603
/// </summary>
585604
public class TandemBicycleClass
586605
{
@@ -604,15 +623,16 @@ public int TandemBicycle(int[] redShirtSpeeds, int[] blueShirtSpeeds, bool faste
604623
return totalSpeed;
605624
}
606625

607-
private void ReverseArrayInPlace(int[] redShirtSpeeds)
626+
private void ReverseArrayInPlace(int[] array)
608627
{
609628
var start = 0;
610-
var end = redShirtSpeeds.Length - 1;
629+
var end = array.Length - 1;
611630
while (start < end)
612631
{
613-
var temp = redShirtSpeeds[start];
614-
redShirtSpeeds[start] = redShirtSpeeds[end];
615-
redShirtSpeeds[end] = temp;
632+
var temp = array[start];
633+
array[start] = array[end];
634+
array[end] = temp;
635+
616636
start += 1;
617637
end -= 1;
618638
}
@@ -622,7 +642,29 @@ private void ReverseArrayInPlace(int[] redShirtSpeeds)
622642

623643
#region OptimalFreelancing
624644
/// <summary>
645+
/// You're recently started freelancing software development and have been offered a variety of job
646+
/// oppotunities. Each job has a deadline ,meaning there is no value in completing the work after the
647+
/// deadline.Additionally, each job has an associate payment representing the profit for completing
648+
/// that job. Givem this information, write a function that returns the maximum profit that can be obtained in
649+
/// a 7-day period.
650+
///
651+
/// Each job will take one full day to complete, and the deadline will be given as the number of days left
652+
/// to complete the job. For example, if a job has deadline of 1, then it can only be completed if it is
653+
/// the first job worked on. if a job has a deadline of 2, then it could be started on the first or second
654+
/// day.
655+
///
656+
///
657+
/// Note: There is no requirement complete all of the jobs. Only one job can be worked on at a time, meaning
658+
/// that in some screnarios it will be impossible to complete them all.
625659
///
660+
/// Samle Input:
661+
/// Jobs =[
662+
/// {"deadline":1,"payment":1},
663+
/// {"deadline":2,"payment":1},
664+
/// {"deadline":2,"payment":2}
665+
/// ]
666+
///
667+
/// Output= 3
626668
/// </summary>
627669
public class OptimalFreelancingClass
628670
{
@@ -631,9 +673,7 @@ public int OptimalFreelancing(Dictionary<string, int>[] jobs)
631673
const int LENGTH_OF_WEEK = 7;
632674
int profit = 0;
633675
Array.Sort(jobs, Comparer<Dictionary<string, int>>.Create((jobOne, JobTwo) => JobTwo["payment"]
634-
.CompareTo(jobOne["payment"])
635-
)
636-
);
676+
.CompareTo(jobOne["payment"])));
637677

638678
bool[] timeline = new bool[LENGTH_OF_WEEK];
639679

@@ -658,7 +698,14 @@ public int OptimalFreelancing(Dictionary<string, int>[] jobs)
658698

659699
#region RemoveDuplicatesFromLinkedList
660700
/// <summary>
661-
///
701+
/// You're given the head of a Singly Linked list whose nodes are in sorted order with respect
702+
/// to their values. Write a function that returns a modified version of the linked List that doesn't
703+
/// contain any nodes with duplicated values. The Linked List should be modified in place (i:e, you
704+
/// shouldn't create a brand new list), and the modified Linked List should still its modified Linked
705+
/// List should still have its nodes sorted with respect to their values.
706+
///
707+
/// Each Linked List node has integer has an integer value as well as a next node pointing to the next
708+
/// node in the list or to Node/null if it's the tail of the list.
662709
/// </summary>
663710
public class RemoveDuplicatesFromLinkedListClass
664711
{
@@ -716,8 +763,8 @@ public LinkedList MiddleNode(LinkedList linkedList)
716763
LinkedList fastnode = linkedList;
717764
while (fastnode != null && fastnode.next != null)
718765
{
719-
slowNode = fastnode.next;
720-
fastnode = fastnode.next;
766+
slowNode = slowNode.next;
767+
fastnode = fastnode.next.next;
721768
}
722769

723770
return slowNode;
@@ -1218,12 +1265,11 @@ public int FirstNonRepeatingCharacter(string str)
12181265
/// </summary>
12191266
public class SemordnilapClass
12201267
{
1221-
public List<List<string>> Semordnilap(string[] words)
1222-
{
1223-
HashSet<string> wordsSet = new HashSet<string>();
1268+
public List<List<string> > Semordnilap(string[] input) {
1269+
HashSet<string> wordsSet = new HashSet<string>(input);
12241270
List<List<string>> semordnilapPairs = new List<List<string>>();
12251271

1226-
foreach (var word in words)
1272+
foreach (string word in input)
12271273
{
12281274
char[] chars = word.ToCharArray();
12291275
Array.Reverse(chars);
@@ -1236,9 +1282,9 @@ public List<List<string>> Semordnilap(string[] words)
12361282
wordsSet.Remove(reverse);
12371283
}
12381284
}
1239-
12401285
return semordnilapPairs;
12411286
}
1287+
12421288
}
12431289
#endregion
12441290

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
14+
<PackageReference Include="xunit" Version="2.4.2" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.2.0">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\Medium_Algorithms\Medium_Algorithms.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.VisualStudio.TestPlatform.TestHost;
2+
using static Medium_Algorithms.MediumAlgorithmsClass;
3+
4+
namespace Medium_Algorithms.Tests
5+
{
6+
public class UnitTest1
7+
{
8+
#region TaskAssignment
9+
[Fact(Skip =" Unit Test failing.")]
10+
public void TaskAssignmentTestCase1()
11+
{
12+
var k = 3;
13+
var tasks = new List<int> { 1, 3, 5, 3, 1, 4 };
14+
var expected = new List<List<int>>();
15+
List<int> subarr = new List<int> { 4, 2 };
16+
List<int> subarr2 = new List<int> { 0, 5 };
17+
List<int> subarr3 = new List<int> { 3, 1 };
18+
expected.Add(subarr);
19+
expected.Add(subarr2);
20+
expected.Add(subarr3);
21+
var actual = new TaskAssignmentClass().TaskAssignment(k, tasks);
22+
for (var i = 0; i < expected.Count; i++)
23+
{
24+
Assert.True(Enumerable.SequenceEqual(expected[i], actual[i]));
25+
}
26+
}
27+
#endregion
28+
}
29+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace Medium_Algorithms
2+
{
3+
public class MediumAlgorithmsClass
4+
{
5+
#region TaskAssignment
6+
/// <summary>
7+
///
8+
/// </summary>
9+
public class TaskAssignmentClass
10+
{
11+
public List<List<int>> TaskAssignment(int k, List<int> tasks)
12+
{
13+
List<List<int>> pairedTasks = new List<List<int>>();
14+
Dictionary<int, List<int>> taskDurationsToIndices = GetTaskDurationsToIndices(tasks);
15+
16+
List<int> sortedTasks = tasks;
17+
sortedTasks.Sort();
18+
19+
for (int index = 0; index < k; index++)
20+
{
21+
int task1Duration = sortedTasks[index];
22+
List<int> indicesWithTask1Duration = taskDurationsToIndices[task1Duration];
23+
int task1Index = indicesWithTask1Duration[indicesWithTask1Duration.Count - 1];
24+
indicesWithTask1Duration.RemoveAt(indicesWithTask1Duration.Count - 1);
25+
26+
int taskSortedIndex = tasks.Count - 1 - index;
27+
int task2Duration = sortedTasks[taskSortedIndex];
28+
List<int> indicesWithTask2Duration = taskDurationsToIndices[task2Duration];
29+
30+
int task2Index = indicesWithTask2Duration[indicesWithTask1Duration.Count - 1];
31+
indicesWithTask1Duration.RemoveAt(indicesWithTask1Duration.Count - 1);
32+
33+
List<int> pairedTask = new List<int>();
34+
pairedTask.Add(task1Index);
35+
pairedTask.Add(task2Index);
36+
pairedTasks.Add(pairedTask);
37+
}
38+
39+
return pairedTasks;
40+
}
41+
42+
private Dictionary<int, List<int>> GetTaskDurationsToIndices(List<int> tasks)
43+
{
44+
Dictionary<int, List<int>> taskDurationsToIndices = new Dictionary<int, List<int>>();
45+
46+
for (int i = 0; i < tasks.Count; i++)
47+
{
48+
int taskDuration = tasks[i];
49+
if (taskDurationsToIndices.ContainsKey(taskDuration))
50+
{
51+
taskDurationsToIndices[taskDuration].Add(i);
52+
}
53+
else
54+
{
55+
List<int> temp = new List<int>();
56+
temp.Add(i);
57+
taskDurationsToIndices[taskDuration] = temp;
58+
}
59+
}
60+
61+
return taskDurationsToIndices;
62+
}
63+
}
64+
#endregion
65+
}
66+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)