Skip to content

Instantly share code, notes, and snippets.

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]];
}
@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
// / | |
@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 / 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 / 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];