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 / NodeDepth.cpp
Created August 18, 2020 05:31
Node Depths
#include <cmath>
// Repasado 2 veces
using namespace std;
class BinaryTree {
public:
int value;
BinaryTree *left;
BinaryTree *right;
@misterpoloy
misterpoloy / isValidSubSequence.cpp
Created August 18, 2020 05:22
Validate Subsequence
using namespace std;
// Repasado 2 veces
bool isValidSubsequence(vector<int> array, vector<int> sequence) {
int i = 0;
int seqIndex = 0;
while (i < array.size()) {
cout << "top= " << array[i] << endl;
cout << "sequence= " << sequence[seqIndex] << endl;
@misterpoloy
misterpoloy / TwONumberSum.cpp
Last active August 18, 2020 05:16
Two Number Sum.cpp
#include <vector>
#include <unordered_set>
using namespace std;
// Repasado 2 veces
vector<int> twoNumberSum(vector<int> array, int targetSum) {
unordered_set<int> targets;
for (int i = 0; i < array.size(); i++) {
int y = targetSum - array[i];
if (targets.find(y) != targets.end()) {
@misterpoloy
misterpoloy / BreadthFirstTraversal.cpp
Created August 18, 2020 04:39
Breadth First Traversal
void traverseLevelOrder() {
if (head == nullptr) return;
std::cout << "-- LEVEL ORDER TRAVERSE" << std::endl;
std::queue<BSTNode*> queue;
queue.push(head);
while (!queue.empty()) {
BSTNode* current = queue.front();
std::cout << queue.front()->value << std::endl;
if (current->left != nullptr) queue.push(current->left);
if (current->right != nullptr) queue.push(current->right);
@misterpoloy
misterpoloy / Sum.js
Created August 12, 2020 18:36
Javascript code challenge sum(1)(2)(3)(4)..( n)()
// sum(1)(2)(3)(4)..( n)()
const sum = (a) => {
return (b) => b ? sum(a + b) : a;
}
console.log(sum(1)(2)(3)(4)());
@misterpoloy
misterpoloy / NumberOfCoinsForChange.cpp
Last active June 25, 2020 19:52
#CodeChallenge Given a set of coins denominations calculate the minimum combinations to get the target amount
#include <vector>
#include <climits>
using namespace std;
// O(dn) time | O(n) space
int minNumberOfCoinsForChange(int n, vector<int> denoms) {
// Create a matrix, to store the best possiblitites until target.
vector<int> numOfCoins(n + 1, INT_MAX); // +1, to include zero.
numOfCoins[0] = 0; // Starts at zero because, how many 0 coins do I need to make 0 in value.
for (int denomination : denoms) {
@misterpoloy
misterpoloy / YoungestAncestor.cpp
Created June 25, 2020 08:46
#CodeChallenge Youngest common ancestor in a graph
#include <vector>
using namespace std;
class AncestralTree {
public:
char name;
AncestralTree *ancestor;
AncestralTree(char name) {
this->name = name;
@misterpoloy
misterpoloy / RiverSizes.cpp
Created June 23, 2020 08:14
#CodeChallenge Get the lenght of all the rivers, rivers are represented by 1 and land by 0
#include <vector>
using namespace std;
vector<vector<int>> getUnvisitedNeighbors(int i, int j, vector<vector<int>> matrix, vector<vector<int>>& visited) {
vector<vector<int>> unvisitedNeighbors{};
// Top
if (i > 0 && !visited[i - 1][j]) {
unvisitedNeighbors.push_back({ i - 1, j});
}
// Bottom
@misterpoloy
misterpoloy / JumpsCycleCheck.cpp
Created June 21, 2020 08:41
#Code Challenge Given a number of arrays each element rempresenting the number of jumps calcultate if starts at zero element
using namespace std;
int getNextIdx(int currentIdx, vector<int>& array) {
int nextIdx = (currentIdx + array[currentIdx]) % (int)array.size();
return nextIdx >= 0 ? nextIdx : nextIdx + array.size();
}
bool hasSingleCycle(vector<int> array) {
int currentIdx = 0;
int numberJumps = 0;
@misterpoloy
misterpoloy / SearchInSortedMatrix.cpp
Created June 9, 2020 04:47
#CodeChallenge Search in sorted matrix use of column index and row index.
#include <vector>
using namespace std;
vector<int> searchInSortedMatrix(vector<vector<int>> matrix, int target) {
int col = matrix[0].size() - 1;
int row = 0;
while (row < matrix.size() && col >= 0) {
if (target < matrix[row][col]) {
col--;