Skip to content

Instantly share code, notes, and snippets.

@pinkmomo027
pinkmomo027 / dijkstra.js
Last active August 9, 2018 17:09
dijkstra.js
let G = [
[0, 2, 5, Infinity],
[2, 0, Infinity, 7 ],
[5, Infinity, 0, Infinity],
[Infinity, 7, Infinity, 0 ]];
let N = G.length;
let target = 3; // target is to visit 3rd
let distance = new Array(N).fill(Infinity);
let visited = new Array(N).fill(false);
@pinkmomo027
pinkmomo027 / pq.js
Created June 29, 2018 23:55
trying out PQ
class QElement {
constructor (value, priority) {
this.value = value;
this.priority = priority
}
}
class PQ {
constructor () {
this.elements = ['root'];
@pinkmomo027
pinkmomo027 / binary.js
Created June 29, 2018 00:25
print binary
let finalResult = [];
function printBinary(number) {
let chosen = [];
printBinaryHelper(number, chosen);
}
function printBinaryHelper(count, chosen) {
if (count == 0) {
// base case
@pinkmomo027
pinkmomo027 / backtrack.js
Created June 29, 2018 00:17
subsets of array
let names = ["fifi", "vampire", "lena", "wheels"];
let finalResult = [];
function sublist(array) {
let chosen = [];
sublistHelper(array, chosen);
}
function sublistHelper(array, chosen) {
if (array.length == 0) {
function travel(temp, level) {
if (count.every(element => element == 0) ) {
result.push(temp.join(''));
return;
}
for (let i = 0; i < count.length; i++) {
if (count[i] > 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;
}
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 / 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);
}
@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 / 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++) {