Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
CarlaTeo / distinctTriangles.js
Last active June 28, 2021 03:05
[JS] Count distinct triagles
// 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];
@CarlaTeo
CarlaTeo / brackets.js
Created June 4, 2021 02:36
[JS] Balanced brackets
const BRACKETS = {
"(": 1,
"[": 2,
"{": 3,
")": -1,
"]": -2,
"}": -3,
};
@CarlaTeo
CarlaTeo / minLenSubString.js
Last active June 27, 2021 23:06
[JS] Minimum Length Substrings
// 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;
@CarlaTeo
CarlaTeo / minPerms.js
Created June 9, 2021 03:16
[JS] Minimize permutations
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)) {
@CarlaTeo
CarlaTeo / nodesInSubTree.js
Last active June 28, 2021 02:23
[JS] Nodes in a Subtree
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]];
@CarlaTeo
CarlaTeo / medianStream.js
Created June 17, 2021 03:48
[JS] Median stream
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])) {
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);
}
@CarlaTeo
CarlaTeo / arrayFlat.js
Created June 22, 2021 23:27
[JS] Recreate arrayFlat
function arrayFlat(array) {
return array.reduce((result, elem) => {
if(Array.isArray(elem)) {
result.push(...arrayFlat(elem));
}
else {
result.push(elem);
}
return result;
}, []);
@CarlaTeo
CarlaTeo / BSTRangeSum.js
Created June 27, 2021 03:32
[JS] BST range sum
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;
}
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;
}