diff --git a/LibStudySesh.sln b/LibStudySesh.sln new file mode 100644 index 0000000..20a6111 --- /dev/null +++ b/LibStudySesh.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibStudySesh", "LibStudySesh.vcxproj", "{3D037F4C-2040-4E91-880E-335F27A3A8ED}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x64.ActiveCfg = Debug|x64 + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x64.Build.0 = Debug|x64 + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x86.ActiveCfg = Debug|Win32 + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x86.Build.0 = Debug|Win32 + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x64.ActiveCfg = Release|x64 + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x64.Build.0 = Release|x64 + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x86.ActiveCfg = Release|Win32 + {3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9AEFFC5C-BDFA-4B81-BC50-F62BD1935871} + EndGlobalSection +EndGlobal diff --git a/LibStudySesh.vcxproj b/LibStudySesh.vcxproj new file mode 100644 index 0000000..e51dfdf --- /dev/null +++ b/LibStudySesh.vcxproj @@ -0,0 +1,139 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {3d037f4c-2040-4e91-880e-335f27a3a8ed} + LibStudySesh + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/LibStudySesh.vcxproj.filters b/LibStudySesh.vcxproj.filters new file mode 100644 index 0000000..0d004b8 --- /dev/null +++ b/LibStudySesh.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/src/leet.cpp b/src/leet.cpp new file mode 100644 index 0000000..3179ac6 --- /dev/null +++ b/src/leet.cpp @@ -0,0 +1,34 @@ +#pragma once +#include "leet.h" + +std::vector> Solution::threeSum(std::vector& nums) { + std::vector> ans; + + //itterate through map O(n^2) and create a map of the sums of each element + for (int i = 0; i < nums.size(); i++) + { + for (int j = i+1; j < nums.size(); j++) + { + sums[nums[i] + nums[j]] = std::vector{i, j}; + } + } + + for (int i = 0; i < nums.size(); i++) + { + if (sums.count(0-nums[i])) + {//If required value is in map + std::vector indexes = sums[0-nums[i]]; + if (i == indexes[0] || i == indexes[1]) + continue; + ans.push_back({ nums[i], nums[indexes[0]], nums[indexes[1]] }); + } + } + + return ans; +} + +// +//Given an integer array nums, return all the triplets[nums[i], nums[j], nums[k]] +// such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. +// +//Notice that the solution set must not contain duplicate triplets. \ No newline at end of file diff --git a/src/leet.h b/src/leet.h new file mode 100644 index 0000000..fdf56f3 --- /dev/null +++ b/src/leet.h @@ -0,0 +1,11 @@ +#pragma once +#include +#include + +class Solution { +public: + std::vector> threeSum(std::vector& nums); +private: + //Map of sum + indexes + std::map> sums; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..909d5ff --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +#include "leet.h" + +template +void swap(T* xp, T* yp) { + T temp = *xp; + *xp = *yp; + *yp = temp; +} + +void selectionSort(int caseArr[], std::string placeArr[], int n) +{ + int i, j, min_idx; + for (i = 0; i < n - 1; i++) + { + min_idx = i; + for (j = i + 1; j < n; j++) + if (caseArr[j] > caseArr[min_idx]) + min_idx = j; + + swap(&caseArr[min_idx], &caseArr[i]); + swap(&placeArr[min_idx], &placeArr[i]); + } +} + +/* +Write code to replace the selection sort function with bubble sort. In +your own words, briefly compare the two sorting algorithms. Use the +code examples to support your claims. +*/ + +void bubbleSort(int caseArr[], std::string placeArr[], int n) +{ + bool flag = true; + + while (flag) { + for (int i = n - 1; i > 0; i--) + { + if (caseArr[i] > caseArr[i - 1]) + {//Swap needs to take place + flag = false; + swap(&caseArr[i], &caseArr[i - 1]); + swap(&placeArr[i], &placeArr[i - 1]); + i+=2; + } + + } + + //Array is sorted + if (flag) + return; + flag = true; + } +} + +/* +Bubble sort has an average time complexity of O(n^2) which is the same as an insertion +sort so as the number of data items grow, both are likely to have same scale in performance. +This means it doesn't really matter which one we pick. They both also have the same memory complexity of +O(1) which is constant as no new memory is allocated during the execution of the program. Because of this, +both algorithms are nearly idendical hence it doesn't really matter which one we choose.*/ + + + +/* +Birmingham 120 +Derbyshire 94 +Essex 182 +Hertfordshire 174 +Kent 157 +Liverpool 102 +Northamptonshire 107 +Nottinghamshire 88 +Staffordshire 89 +Wolverhampton 89 +*/ + + +int main() { + + /*std::string UTLANames[] = { "Birmingham", "Derbyshire", "Essex", + "Hertfordshire", "Kent", "Liverpool", + "Northamptonshire", "Nottinghamshire", + "Staffordshire", "Wolverhampton" }; + int CovidCases[] = { 120, 94, 182, 174, 157, 102, 107, 88, 89, 89 }; + + bubbleSort(CovidCases, UTLANames, 10); + + for (size_t i = 0; i < 10; i++) + { + std::cout << std::left << std::setw(20) << UTLANames[i] << CovidCases[i] << " Cases\n"; + } + + std::cout << "Hello, World!";*/ + std::vector nums{ -1, 0, 1, 2, -1, -4 }; + Solution s; + std::vector> ans = s.threeSum(nums); +} \ No newline at end of file