Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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) {
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 / 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]) {
@pinkmomo027
pinkmomo027 / sum to zero.js
Created June 17, 2018 23:11
find triplets summing to 0
const arr = [0, -1, 2, -3, 1];
// [ -3, -1, 0, 1, 2 ]
function triplets(arr) {
let sorted = arr.sort((a, b) => { return a > b; });
let answer = [];
for(p1 = 0; p1 < sorted.length - 2; p1++) {
@pinkmomo027
pinkmomo027 / count consecutive numbers.js
Created June 17, 2018 23:28
count consecutive number
const arr = [-1, -1, 1, 1, 1, 1, 7, 11, 11];
// [ -1, 2, 1, 4, 7, 1, 11, 2];
class Stack {
constructor() {
this.values = [];
}
push(v) {
@pinkmomo027
pinkmomo027 / delete consecutive strings.js
Created June 18, 2018 17:53
delete consecutive strings
// https://www.geeksforgeeks.org/delete-consecutive-words-sequence/
function deleteConsecutive(arr) {
let stack = new Stack();
arr.forEach(element => {
if (element == stack.peek()) {
stack.pop();
} else {
stack.push(element);
}
function basicCalc(str) {
let result = 0, length = str.length, sign = 1, char;
let stack = [];
for (let i = 0; i < length; i++) {
char = str.charAt(i);
if (char >= '0') {
//find all the chars
let num = 0;
@pinkmomo027
pinkmomo027 / indexOf.js
Created June 21, 2018 23:58
needle and haysack
function indexOf(string, query) {
for (let i = 0; i < string.length; i++) {
//str.charAt(i)
for (let q = 0; q < query.length; q++) {
if (string[i+q] != query[q]) {
break;
}