Skip to content

Instantly share code, notes, and snippets.

@pinkmomo027
pinkmomo027 / array intersection.js
Created June 17, 2018 23:00
find common elements
const arr1 = [1, 3, 5, 7, 11, 17, 21];
const arr2 = [-1, 0, 2, 3, 9, 11, 29];
function intersection(arr1, arr2) {
let result = [];
let p1 = 0, p2 = 0;
while((p1 < arr1.length) && (p2 < arr2.length)) {
if (arr1[p1] == arr2[p2]) {
function stockSpan(array) {
let answer = [];
array.forEach((stock, index) => {
let count = 1;
for(let i = index - 1; (i >= 0) && (array[i] <= stock); i--) {
count++;
}
@pinkmomo027
pinkmomo027 / infixToPostfix.js
Last active June 14, 2018 16:56
infix to postfix, without parenthesis
// a + b * c + d
// a b c * + d +
// a + b * c + d
// a b c * + d +
function infixToPostfix(input) {
@pinkmomo027
pinkmomo027 / standard LCA.js
Created June 13, 2018 19:05
standard recursive, one run, LCA
class BNode {
constructor (value) {
this.data = value;
this.left = null;
this.right = null;
}
search(values) {
let c = values.indexOf(this.data) >=0 ? 1 : 0;
@pinkmomo027
pinkmomo027 / LCA.js
Last active June 13, 2018 19:04
LCA
class BNode {
constructor (value) {
this.data = value;
this.left = null;
this.right = null;
}
search(values) {
let c = values.indexOf(this.data) >=0 ? 1 : 0;
@pinkmomo027
pinkmomo027 / lowestAncestor.js
Created June 13, 2018 04:00
Lowest Ancestor
class Node {
constructor(value) {
this.value = value;
this.children = [];
}
add(child) {
this.children.push(child);
}
@pinkmomo027
pinkmomo027 / path.js
Last active June 13, 2018 03:51
find path to a node
class Node {
constructor(value) {
this.value = value;
this.children = [];
}
add(child) {
this.children.push(child);
}
@pinkmomo027
pinkmomo027 / clonetree.js
Last active June 13, 2018 00:06
Clone Tree
class Node {
constructor(value) {
this.value = value;
this.children = [];
}
add(child) {
this.children.push(child);
}
@pinkmomo027
pinkmomo027 / TrieNode.js
Last active June 12, 2018 20:41
Trie Node
class TrieNode {
constructor (value, note="") {
this.value = value;
this.note = note;
this.children = new Array(26);
}
addChildNode (char, value=null) {
let index = char.charCodeAt(0) - 97;
this.children[index] = this.children[index] || new TrieNode(null, char);
function traverse(node, level=0) {
let indent = "";
for (let i = 0; i < level; i++) {
indent += "*";
}
console.log(`${indent}${node.nodeName}`);
if (node.childNodes.length > 0 ){
node.childNodes.forEach(traverse, level+1);
}
}