This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for singly-linked list. | |
| * function ListNode(val) { | |
| * this.val = val; | |
| * this.next = null; | |
| * } | |
| */ | |
| /** | |
| * @param {ListNode} head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |