Skip to content

Instantly share code, notes, and snippets.

@cfrank
cfrank / underground_system.js
Created May 31, 2023 23:51
1396. Design Underground System
class UndergroundSystem {
#stationTripLengthMap;
#inFlightTrips;
constructor() {
this.#stationTripLengthMap = {};
this.#inFlightTrips = {};
}
checkIn(id, stationName, t) {
@cfrank
cfrank / max-vowels-substring.js
Created June 1, 2023 18:21
1456. Maximum Number of Vowels in a Substring of Given Length
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
/**
* @param {string} str
* @param {number} frameSize
* @return {number}
*/
var maxVowels = function(str, frameSize) {
// Given a char return true if it's a vowel
const isVowel = (char) => VOWELS.includes(char);
@cfrank
cfrank / longest-zig-zag-binary-tree.ts
Last active June 5, 2023 17:36
1372. Longest ZigZag Path in a Binary Tree
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
@cfrank
cfrank / can-place-flowers.ts
Created June 1, 2023 20:21
605. Can Place Flowers
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
let openPlots = 0;
for (let i = 0; i < flowerbed.length; ++i) {
if (flowerbed[i] === 1) {
continue;
}
const leftSideFree = (i === 0 || flowerbed[i - 1] === 0);
const rightSideFree = (i + 1 >= flowerbed.length || flowerbed[i + 1] === 0);
@cfrank
cfrank / shortest-path-binary-matrix.ts
Created June 2, 2023 19:07
1091. Shortest Path in Binary Matrix
function shortestPathBinaryMatrix(grid: number[][]): number {
const getDirections = ([row, col]: [number, number]): number[][] => {
const canMove = ([row, col]: [number, number]) => {
return (row >= 0 && row < grid.length) && (col >= 0 && col < grid[row].length)
}
// Given a 3x3 matrix with row/col index of [1,1]
// we should return:
//
// [0, 1] == North