Skip to content

Instantly share code, notes, and snippets.

View misterpoloy's full-sized avatar
👽
Software Engineer

Juan P. Ortiz misterpoloy

👽
Software Engineer
View GitHub Profile
@misterpoloy
misterpoloy / PowerSetFromArray.cpp
Created June 9, 2020 03:21
#CodeChallenge return all power sets from a given array, powersets include number cero.
#include <vector>
using namespace std;
// O(n * 2^n) time | O(n * 2^n) space
vector<vector<int>> helper(vector<int> array, int idx) {
if (idx < 0) return {{}}; // Return empty array
vector<vector<int>> subsets = helper(array, idx - 1);
int length = subsets.size();
int word = array[idx]; // starts from last element
for (int j = 0; j < length; j++) {
@misterpoloy
misterpoloy / SameBSTFromArray.cpp
Last active June 7, 2020 04:35
#Code Challenge without construct a bst validate from an array if two are equal consotructed
#include <vector>
using namespace std;
vector<int> getBiggerOrEqual(vector<int> array) {
vector<int> biggerOrEqual = {};
for (int i = 1; i < array.size(); i++) {
if (array[i] >= array[0]) {
biggerOrEqual.push_back(array[i]);
}
@misterpoloy
misterpoloy / groupAnagrams.cpp
Created June 5, 2020 19:59
#Code Challenge, Group all the anagrams of strings given an array
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
vector<vector<string>> groupAnagrams(vector<string> words) {
vector<vector<string>> response;
unordered_map<string, vector<string>> tables;
for (string word : words) {
@misterpoloy
misterpoloy / LongestPalindrome.cpp
Created June 5, 2020 02:56
#CodeChalenge AlgoExpert Longest palindrome in a string
using namespace std;
vector<int> compareHelper(string str, int startIndex, int endIndex) {
while (startIndex >= 0 && endIndex < str.size()) {
if (str[startIndex] != str[endIndex]) {
break;
}
startIndex--;
endIndex++;
}
@misterpoloy
misterpoloy / DecimaToBinaryUsingBitwise.js
Created May 27, 2020 04:45
#CodeChallenge Bitwise Operator
const getBinaryDigit = (target, bytes = 31) => {
let result = "";
for(let i = 0; i < bytes; i++) {
const condition = target & (1 << i);
if (condition != 0) {
result = 1 + result;
} else {
result = 0 + result;
}
}
@misterpoloy
misterpoloy / AparmentHunting.cpp
Last active May 27, 2020 02:09
#CodeChallenge Aparment hunting
#include <vector>
#include <unordered_map>
using namespace std;
int distanceBetwen(int a, int b) {
return abs(a - b);
}
int getIndexOfMinValue(vector<int>& arr) {
@misterpoloy
misterpoloy / LargestRange.cpp
Created May 24, 2020 21:21
#CodeChallenge Longest continues range from start to end in an array
#include <vector>
#include <unordered_map>
#include <climits>
using namespace std;
vector<int> largestRange(vector<int> array) {
vector<int> longestRange;
unordered_map<int, bool> arrayMap;
int longestLength = INT_MIN;
@misterpoloy
misterpoloy / SubArraySort.cpp
Created May 24, 2020 03:00
#CodeChallenge min index and max of an sub array to sort the entire array
#include <vector>
#include <climits>
using namespace std;
bool isInOrder(int i, vector<int>& array) {
// Left most
if (i == 0) return array[i] < array[i + 1];
// Right most
if (i == array.size() - 1) return array[i] > array[i - 1];
@misterpoloy
misterpoloy / FourNumberSum.cpp
Created May 20, 2020 02:55
Four number Sum #CodeChallenge
#include <vector>
#include <unordered_map>
using namespace std;
vector<vector<int>> fourNumberSum(vector<int> array, int targetSum) {
// Write your code here
unordered_map<int, vector<vector<int>>> allPairSums;
vector<vector<int>> quadruplets{};
// For every element apply the next algorithm
@misterpoloy
misterpoloy / Permutations.cpp
Created May 18, 2020 04:32
Permutations of a given arrat #Codechallenge
#include <vector>
using namespace std;
// METHOD 1
// Ypper Bound: O(n! * n * n) time | O(n*n!) space
void getPermutations(vector<int> array, vector<int> currentPerm, vector<vector<int>>& permutations) {
if (array.empty() && currentPerm.size() > 0) {
permutations.push_back(currentPerm); // This get fired n! times
}