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
// People representing each index: | |
// 0 - Carla, 1- Star, 2- Gabriel, 3- Ikeda, 4-Matt | |
// Each food will be represented by its initial: | |
// Banana, Goiabinha, Hersheys, Pacoquinha, Suflair | |
// The solution is a string (eg: 'BGHPS') assigning each food to a person | |
const day = [['S', 'P', 'H', 'B', 'G'], 2]; | |
const max = [['S', 'B', 'G', 'P', 'H'], 2]; | |
const sab = [['S', 'G', 'P', 'B', 'H'], 2]; | |
const gab = [['S', 'H', 'B', 'G', 'P'], 3]; | |
const coringa = [['H', 'G', 'B','P', 'S'], 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
** https://leetcode.com/problems/kth-smallest-element-in-a-bst/ | |
* Definition for a binary tree node. | |
* function TreeNode(val, left, right) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.left = (left===undefined ? null : left) | |
* this.right = (right===undefined ? null : right) | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root |
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
/** | |
* @param {number} N | |
* @param {number} M | |
* @param {number[]} C | |
*/ | |
function getMinCodeEntryTime(N, M, C) { | |
function minDist(i, j) { | |
const path1 = Math.abs(j - i); | |
const bigger = Math.max(i, j); | |
const smaller = Math.min(i, j); |
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
class Node { | |
constructor(value, children = []) { | |
this.value = value; | |
this.children = children; | |
} | |
} | |
// 3 --->7 | |
// / | | |
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 countSort(array) { | |
const counts = {}; | |
for(const num of array) { | |
if(counts[num]) counts[num] += 1; | |
else counts[num] = 1; | |
} | |
for(let idx = 0; idx < Object.keys(counts).length - 1; idx++) { | |
counts[Object.keys(counts)[idx + 1]] += counts[Object.keys(counts)[idx]]; | |
} |
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 binarySearch(array, value) { | |
let i = 0; | |
let j = array.length - 1; | |
while(i <= j) { | |
const middle = Math.floor((j + i) / 2); | |
if(array[middle] === value) return middle; | |
if(array[middle] > value) j = middle - 1; | |
else i = middle + 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 getBSTSum(node, range) { | |
let sum = 0; | |
if(!node) return sum; | |
const [min, max] = range; | |
if(node.left) sum += getBSTSum(node.left, range); | |
if(node.val >= min) { | |
if(node.val <= max) sum += node.val; | |
else return sum; | |
} |
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 arrayFlat(array) { | |
return array.reduce((result, elem) => { | |
if(Array.isArray(elem)) { | |
result.push(...arrayFlat(elem)); | |
} | |
else { | |
result.push(elem); | |
} | |
return result; | |
}, []); |
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 check1EditPalindrome(word, canRemove = true) { | |
if(word.length <= 1) return true; | |
if(word[0] === word[word.length - 1]) { | |
return check1EditPalindrome(word.slice(1, word.length - 1)); | |
} | |
else if(canRemove) { | |
canRemove = false; | |
return check1EditPalindrome(word.slice(1), canRemove) || check1EditPalindrome(word.slice(0, word.length - 1), canRemove); | |
} |
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 getParent(i) { | |
return Math.floor(i + 1 / 2) - 1; | |
} | |
function addHeap(heap, val, comparator) { | |
heap.push(val); | |
let currentIdx = heap.length - 1; | |
let parentIdx = getParent(currentIdx); | |
while(currentIdx > 0 && comparator(heap[currentIdx], heap[parentIdx])) { |
NewerOlder