Skip to content

Instantly share code, notes, and snippets.

@lienista
lienista / ctci-3.6-animal-shelter.js
Last active September 14, 2018 04:36
(Algorithms in Javascript) CTCI 3.6. Animal Shelter: An animal shelter, which holds only dogs and cats, operates on a strictly "first in, first out" basis. People must adopt either the "oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of…
// 3.6. Animal Shelter:
// An animal shelter, which holds only dogs and cats, operates on a strictly "first in, first out" basis.
// People must adopt either the "oldest" (based on arrival time) of all animals at the shelter, or they
// can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type).
// They cannot select which specific animal they would like. Create the data structures to maintain this
// system and implement operations such as enqueue, dequeueAny, deQueueDog, and deQueueCat.
// You may use the built-in LinkedList data structure.
//
// Solution:
// We keep an ID to denote the oldest animal and create separate arrays to enqueue and dequeue.
@lienista
lienista / ctci-3.5-sort-stacks.js
Created September 14, 2018 01:26
(Algorithms in Javascript) CTCI 3.5. Sort Stacks: Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty.
// 3.5. Sort Stacks:
// Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary
// stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the
// following operations: push, pop, peek, and isEmpty.
//
// Solution:
// We use a temporary stack and use it to move elements from input stack.
// Once we encounter an element from input stack that is smaller element at the top of the tempStack, we then move all // the elements back to the original stack. We insert that single element, and then start popping all elements from
// the original stack.
// At the end, we are left with a temporary stack that is sorted in ascending order, so we need to pop all elements
@lienista
lienista / ctci-3.4-queue-via-stacks.js
Created September 13, 2018 21:18
(Algorithms in Javascript) CTCI 3.4 - Queue via Stacks: Implement a MyQueue class which implements a queue using two stacks.
//We create 2 stacks one for enqueue and one for dequeue.
//Once dequeue stack is empty, we pull all elements from enqueue stack onto dequeue stack;
class MyQueue {
constructor(capacity){
this._capacity = capacity || Infinity;
this._dequeue = [];
this._enqueue = [];
}
enqueue(element){
@lienista
lienista / ctci-3.3-stack-of-plates.js
Last active September 13, 2018 01:30
(Algorithms in Javascript) CTCI 3.3. Stack of Plates: Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stack…
class StackOfPlates {
constructor(capacity) {
if(!capacity) {
return 'Specify plate capacity. Cannot be created'; //
}
this._capacity = capacity;
this._storage = [[]]; //Each array represents a set of plates
}
getLastStack(){
@lienista
lienista / ctci-3.1-three-in-one.js
Last active September 12, 2018 02:18
(Algorithms in Javascript) CTCI 3.1. Three In One: Describe how you could use a single array to implement three stacks.
class TripleStack {
constructor(capacity) {
this._capacity = [capacity, capacity, capacity] || [Infinity, Infinity, Infinity]; // capacity of each stack.
this._storage = []; // array used to manage 3 stacks;
this._startIndex = [0,0,0]; // size of each stack,
}
//push element into stack
push(stack, element){
let stack = stack || 0;
@lienista
lienista / leetcode-139-word-break.js
Last active June 18, 2021 01:09
(Algorithms in Javascript) Leetcode 139. Word Break - Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
// Algorithms in Javascript
// Leetcode 139. Word Break: https://leetcode.com/problems/word-break/
// Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
// Note:
// The same word in the dictionary may be reused multiple times in the segmentation.
// You may assume the dictionary does not contain duplicate words.
const wordBreak = (s, wordDict) => {
if(!wordDict) return false;
@lienista
lienista / 54-print-spiral-matrix.js
Last active February 22, 2023 21:54
(Algorithms in Javascript) Leetcode 54. Print Spiral Matrix - Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
const spiralOrder = (matrix) => {
if(!matrix.length || !matrix[0].length){
return [];
}
//Use 4 pointes to create wall around square
let rowBegin = 0,
rowEnd = matrix.length - 1,
colBegin = 0,
colEnd = matrix[0].length - 1;
@lienista
lienista / 141-linked-list-detect-cyle.js
Last active December 5, 2021 04:39
(Algorithms in Javascript) Leetcode 141. Linked List Cycle - Given a linked list, determine if it has a cycle in it.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
const isPrime = (n) => {
if(n<3) return false;
if(n%2===0) return false;
if(n%3===0) return false;
let limit = Math.sqrt(n);
for(let i=5; i<=limit; i+=6){
if(num%i ===0) return false;
if(num%(i+2) ===0) return false;
}
@lienista
lienista / 242-leetcode-valid-anagram.js
Last active February 25, 2021 05:35
(Algorithms in Javascript) Leetcode 242. Valid Anagram - Given two strings s and t , write a function to determine if t is an anagram of s.
const isAnagram => (s, t) {
var lens = s.length,
lent = t.length;
if(lens !== lent) return false;
if(typeof s !== 'string' || typeof t !== 'string') return false;
if(lens === 0 && lent === 0) return true;
var charmap = {};
for(let i=0; i<lens; i++){
charmap[s[i]] = charmap[s[i]] ? charmap[s[i]]+1: 1;