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
var StreamChecker = function(words) { | |
this.tree = PrefixTree(); | |
this.maxWordLength = 0; | |
words.forEach((word) => { | |
this.tree.add(word.split('').reverse().join('')); | |
this.maxWordLength = Math.max(this.maxWordLength, word.length); | |
}); | |
this.storage = []; // should be a queue! This is O(n) time as opposed to a queues O(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
/** | |
* Initialize your data structure here. | |
*/ | |
var AllOne = function() { | |
this.counter = new Map(); // [key: int] | |
this.index = new Map(); // [int: ListNode] | |
this.head = new ListNode(); // prev, next, set<key> | |
this.tail = new ListNode(); | |
this.head.prev = this.tail; | |
this.tail.next = this.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
/** | |
* @param {number} capacity | |
*/ | |
var LRUCache = function(capacity) { | |
this.size = 0; | |
this.capacity = capacity; | |
this.index = new Map(); // <key: ListNode> | |
this.head = new ListNode(null, null); | |
this.tail = new ListNode(null, null); |
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
/** | |
* @param {number[][]} obstacleGrid | |
* @return {number} | |
*/ | |
var uniquePathsWithObstacles = function(obstacleGrid) { | |
const memo = new Array(obstacleGrid.length) | |
.fill(0) | |
.map(() => new Array(obstacleGrid[0].length).fill(null)); | |
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
/** | |
* @param {number[][]} grid | |
* @return {number} | |
*/ | |
var uniquePathsIII = function(grid) { | |
const visited = new Array(grid.length).fill(0).map(() => new Array(grid[0].length).fill(0)); | |
let visitedCount = 0; | |
const squaresToVisit = countNonObstacleSquares(grid); | |
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
/** | |
* @param {number[]} nums | |
* @return {string} | |
*/ | |
var largestNumber = function(nums) { | |
const result = nums | |
.map((num) => num.toString()) | |
.sort((a, b) => { | |
const aBeforeB = parseInt(`${a}${b}`, 10); |
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 Queue = function(){ | |
this.storage = new Array(10); | |
this.start = -1; // index of first element in array | |
this.end = -1; // index of last element in array | |
}; | |
Queue.prototype.size = function(){ | |
if(this.start === -1) return 0; | |
if(this.start < this.end) return this.end - this.start + 1; | |
if(this.start === this.end) return 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
/** | |
* @param {number[]} nums | |
* @param {number} k | |
* @return {number[]} | |
*/ | |
var maxSlidingWindow = function(nums, k) { | |
if(!nums.length || !k) return []; // because leetcode | |
const result = []; |
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
var isOneEditDistance = function(s, t) { | |
if(Math.abs(s.length - t.length) > 1) return false; | |
if(s === t) return false; | |
let longer; let shorter; | |
if(s.length > t.length){ | |
[longer, shorter] = [s, t]; | |
}else{ | |
[longer, shorter] = [t, s]; | |
} |
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
// recursive | |
var calculateMinimumHP = function(dungeon) { | |
const [m, n] = [dungeon.length, dungeon[0].length]; | |
const dp = new Array(m).fill(0).map(() => new Array(n).fill(null)); | |
const traverse = (i, j) => { | |
if(i === m) return Infinity; | |
if(j === n) return Infinity; |