Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
CarlaTeo / findTruth.js
Last active August 10, 2022 03:13
[JS] Descobrir verdade com base em pitacos
// 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];
@CarlaTeo
CarlaTeo / kthSmallest.js
Created July 30, 2022 14:13
[JS] Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
** 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
@CarlaTeo
CarlaTeo / getMinCodeEntryTime.js
Created July 15, 2022 23:51
[JS] Double Rotary Lock
/**
* @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);
@CarlaTeo
CarlaTeo / DPS_BFS.js
Last active July 12, 2022 03:52
[JS] Depth First Search and Breadth First Search
class Node {
constructor(value, children = []) {
this.value = value;
this.children = children;
}
}
// 3 --->7
// / | |
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]];
}
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;
}
@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;
}
@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;
}, []);
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 / 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])) {