Mad Library Sesh
This commit is contained in:
34
src/leet.cpp
Normal file
34
src/leet.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include "leet.h"
|
||||
|
||||
std::vector<std::vector<int>> Solution::threeSum(std::vector<int>& nums) {
|
||||
std::vector<std::vector<int>> 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<int>{i, j};
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < nums.size(); i++)
|
||||
{
|
||||
if (sums.count(0-nums[i]))
|
||||
{//If required value is in map
|
||||
std::vector<int> 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.
|
11
src/leet.h
Normal file
11
src/leet.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<std::vector<int>> threeSum(std::vector<int>& nums);
|
||||
private:
|
||||
//Map of sum + indexes
|
||||
std::map<int, std::vector<int>> sums;
|
||||
};
|
103
src/main.cpp
Normal file
103
src/main.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
|
||||
#include "leet.h"
|
||||
|
||||
template<typename T>
|
||||
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<int>(&caseArr[min_idx], &caseArr[i]);
|
||||
swap<std::string>(&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<int>(&caseArr[i], &caseArr[i - 1]);
|
||||
swap<std::string>(&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<int> nums{ -1, 0, 1, 2, -1, -4 };
|
||||
Solution s;
|
||||
std::vector<std::vector<int>> ans = s.threeSum(nums);
|
||||
}
|
Reference in New Issue
Block a user