This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// exercise 2 | |
const payload = { | |
width: 360, | |
height: 300, | |
locale: 'en', | |
toolbar_bg: '', | |
interval: '3h', | |
pair: 'BTC_USD', | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var minSwaps = function(s) { | |
let close = 0 | |
let maxClose = 0 | |
for (const letter of s.split('')) { | |
if (letter == "[") { | |
close -= 1 | |
} else { | |
close += 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) && |
NewerOlder