Skip to content

Instantly share code, notes, and snippets.

View RP-3's full-sized avatar

Rohan Rogers RP-3

  • San Francisco Bay Area
View GitHub Profile
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)
};
/**
* 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;
/**
* @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);
/**
* @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));
/**
* @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);
/**
* @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);
@RP-3
RP-3 / Queue.js
Created February 17, 2020 22:23
O(1) Queue in Javascript.
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;
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var maxSlidingWindow = function(nums, k) {
if(!nums.length || !k) return []; // because leetcode
const result = [];
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];
}
// 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;