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 CircularQueue { | |
| /** | |
| * @param {number} growthRate Rate of growth when additional allocation is required. It must be larger than 1. | |
| */ | |
| constructor(growthRate) { | |
| this.growthRate = growthRate | |
| this.data = new Array(4); | |
| this.head = 0; | |
| this.tail = 0; | |
| this.size = 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
| class Trie { | |
| constructor() { | |
| this.root = { children: {} }; | |
| this.size = 0; | |
| } | |
| add(key, value) { | |
| let node = this.root; | |
| for (let i = 0; i < key.length; ++i) { | |
| if (!node.children[key[i]]) node.children[key[i]] = { children: {} }; |
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
| import { useEffect, useReducer, useRef, useState } from 'react' | |
| interface DialationState { | |
| counter: number | |
| called: number | |
| resolved: number | |
| timer?: NodeJS.Timeout | |
| isCalling: boolean | |
| } |
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 Heap { | |
| constructor(comparator) { | |
| this.data = [] | |
| this.comparator = comparator | |
| } | |
| push(value) { | |
| this.data.push(value) | |
| this.siftUp(this.size() - 1) | |
| return value |
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
| import { CircularQueue } from './somewhere' | |
| export enum SearchEvaluation { | |
| SUCCESS = 'SUCCESS', | |
| KEEP = 'KEEP', | |
| TERMINATE = 'TERMINATE', | |
| } | |
| export interface SearchChoice<S> { | |
| nextState: 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
| // Let N := Z+ ∪ {0} | |
| // N → Z | |
| export function mapNaturalToInteger(x: number): number { | |
| return x % 2 === 0 ? x / 2 : -(x + 1) / 2 | |
| } | |
| // Z → N | |
| export function mapIntegerToNatural(x: number): number { | |
| return x >= 0 ? x * 2 : -2 * x - 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
| type Id = number | |
| export interface Dependency { | |
| id: Id | |
| dependencies: Id[] | |
| } | |
| const PENDING = 1 | |
| const WRITTEN = 2 |
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
| type KeyType = number | string | symbol | |
| export interface DFSProcess<T> { | |
| state: T | |
| description?: string | |
| } | |
| export interface DFSNode<T> { | |
| id: KeyType | |
| state: T |
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
| /** | |
| * Extract n samples from list without replacement in O(|list|) | |
| * @param {Object[]} list - array containing elements | |
| * @param {number} n - size of the extraction | |
| * @returns {Object[]} new array contains extracted elements | |
| */ | |
| function getSamplesWithoutReplacement<T>(list: T[], n: number): T[] { | |
| if (n <= 0) return [] | |
| list = [...list] |
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
| export type Relation = [string, string] | |
| function makeGraph(relations: Relation[]): Record<string, string[]> { | |
| const result: Record<string, string[]> = {} | |
| relations.forEach(([u, v]) => { | |
| (result[u] ??= []).push(v) | |
| result[v] ??= [] | |
| }) | |
| return result | |
| } |
OlderNewer