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 / generateURL.js
Last active January 28, 2022 04:39
Create a function `generateUrl` to generate a URL from given parameters
// exercise 2
const payload = {
width: 360,
height: 300,
locale: 'en',
toolbar_bg: '',
interval: '3h',
pair: 'BTC_USD',
}
@misterpoloy
misterpoloy / QuickSort.js
Created October 8, 2021 20:07
Quick Sort implementation recursive in Javascript
function swap(idxA, idxB, array) {
const temp = array[idxA]
array[idxA] = array[idxB]
array[idxB] = temp
}
// A startIdx will search for elements greather than pivor
// B endIdx will search for elements less than pivor
function quickSortHelper(startIdx, endIdx, array) {
@misterpoloy
misterpoloy / ClassPhotos.js
Created October 7, 2021 21:50
Class photos Greedy Method
function classPhotos(redShirtHeights, blueShirtHeights) {
// Sort by height the students Log(n)
redShirtHeights.sort((a, b) => a - b)
blueShirtHeights.sort((a, b) => a - b)
// Pick up who's going to be the backrow
const backColor = (redShirtHeights[0] > blueShirtHeights[0]) ? "RED" : "BLUE"
// compare every classmate and validate it's possible
for (let i = 0; i < redShirtHeights.length; i++) {
@misterpoloy
misterpoloy / minSwaps.js
Created October 7, 2021 21:21
Min number of swaps greedy
var minSwaps = function(s) {
let close = 0
let maxClose = 0
for (const letter of s.split('')) {
if (letter == "[") {
close -= 1
} else {
close += 1
}
@misterpoloy
misterpoloy / MinimunWaitingTime.js
Created October 7, 2021 06:47
Mínimum waiting time Greedy Method
function minimumWaitingTime(queries) {
let total = 0
// Sort the array
queries.sort((a, b) => a - b)
for (let i = 0; i < queries.length; i++) {
const remainOperations = queries.length - 1 - i;
total += remainOperations * queries[i]
}
return total
}
@misterpoloy
misterpoloy / RemoveDuplicateValuesLinkedList.js
Created October 6, 2021 23:44
Remove duplciates from a sorted linked list
// This is an input class. Do not edit.
class LinkedList {
constructor(value) {
this.value = value;
this.next = null;
}
}
// O(n) time | O(1) space
function removeDuplicatesFromLinkedList(linkedList) {
@misterpoloy
misterpoloy / breadthFirstSearch.js
Created October 6, 2021 05:50
breadthFirstSearch AlgoExpert Implementation
function breadthFirstSearch(array) {
const queue = [this]
while (queue.length) {
const current = queue.shift()
array.push(current.name)
for (const child of current.children) {
queue.push(child)
}
}
return array
@misterpoloy
misterpoloy / FindNLargestValueInBST.js
Created September 6, 2021 23:32
Find k largest value in a binary search tree using javascript
function postOrderTraverse(tree, target, nodeInfo) {
if (tree === null || nodeInfo.visited >= target) return
postOrderTraverse(tree.right, target, nodeInfo)
if (nodeInfo.visited < target) {
nodeInfo.visited = nodeInfo.visited + 1
nodeInfo.lastValue = tree.value
postOrderTraverse(tree.left, target, nodeInfo)
}
}
@misterpoloy
misterpoloy / GetDiameter.js
Created September 6, 2021 22:42
Get diameter of a binary tree with javascript recursive impelemntation
function getDiameterAndHeight(tree) {
if (tree === null) return { diameter: 0, height: 0 }
const leftTreeInfo = getDiameterAndHeight(tree.left)
const rightTreeInfo = getDiameterAndHeight(tree.right)
const diameterFromRoot = leftTreeInfo.height + rightTreeInfo.height
const longestDiameterSoFar = Math.max(leftTreeInfo.diameter, rightTreeInfo.diameter)
const diameter = Math.max(diameterFromRoot, longestDiameterSoFar)
const height = Math.max(leftTreeInfo.height, rightTreeInfo.height) + 1
@misterpoloy
misterpoloy / TreeIsBalanced.js
Created September 6, 2021 22:07
Return if a tree is balanced, difference is not greater than 1 in javascript
// Return Node info
function getNodeHeightBalanced(tree) {
if (tree === null) return { isBalanced: true, height: -1}
const leftTreeInfo = getNodeHeightBalanced(tree.left)
const rightTreeInfo = getNodeHeightBalanced(tree.right)
const isBalanced = (
// Both left and right sub-tree should be balanced
(leftTreeInfo.isBalanced && rightTreeInfo.isBalanced) &&