This file contains hidden or 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
| // Given a list of N triangles with integer side lengths, determine how many different triangles there are. | |
| // Two triangles are considered to be the same if they can both be placed on the plane such that their vertices | |
| // occupy exactly the same three points. | |
| // https://leetcode.com/discuss/interview-question/922155/facebook-recruiting-portal-counting-triangles | |
| function sortTriangle([a, b, c]) { | |
| if(b > a) { | |
| if(c > b) return [c, b, a]; | |
| else if(c > a) return [b, c, a]; | |
| else return [b, a, c]; |
This file contains hidden or 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
| const BRACKETS = { | |
| "(": 1, | |
| "[": 2, | |
| "{": 3, | |
| ")": -1, | |
| "]": -2, | |
| "}": -3, | |
| }; | |
This file contains hidden or 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
| // Solution |s|**2 + |t| | |
| function containsSubstring(str, startIdx, endIdx, substrMap) { | |
| const substrMapCopy = {...substrMap}; | |
| for(let i = startIdx; i <= endIdx; i++) { | |
| const char = str[i]; | |
| if(substrMapCopy[char]) { | |
| substrMapCopy[char] -= 1; | |
| if(substrMapCopy[char] === 0) { | |
| delete substrMapCopy[char]; | |
| if(!Object.keys(substrMapCopy).length) return true; |
This file contains hidden or 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 getPermutations(arr, minOp, original = []) { | |
| const result = []; | |
| for(let size = 2; size <= arr.length; size++) { | |
| for(let i = 0; i + size <= arr.length; i ++) { | |
| const permutation = [ | |
| ...arr.slice(0, i), | |
| ...arr.slice(i, i + size).reverse(), | |
| ...arr.slice(i + size) | |
| ]; | |
| if(!isEqual(original, permutation)) { |
This file contains hidden or 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 Node(val, children) { | |
| this.val = val === undefined ? 0 : val; | |
| this.children = children === undefined ? [] : children; | |
| }; | |
| // BFS | |
| function getNOfDescendants(rootNode, targetVal, startNodeIdx, string) { | |
| let nOfDescendants = 0; | |
| const queue = [[rootNode, startNodeIdx === rootNode.val]]; |
This file contains hidden or 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])) { |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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; | |
| } |