|
| 1 | +{ |
| 2 | + "language": "code_typescript", |
| 3 | + "groups": [ |
| 4 | + [0, 100], |
| 5 | + [101, 300], |
| 6 | + [301, 600], |
| 7 | + [601, 9999] |
| 8 | + ], |
| 9 | + "quotes": [ |
| 10 | + { |
| 11 | + "text": "export class TreeNode<T> {\n\tvalue?: T;\n\tparent?: TreeNode<T>;\n\tchildren: TreeNode<T>[];\n\n\tconstructor(value?: T, opts?: { parent?: TreeNode<T>, children?: TreeNode<T>[] }) {\n\t\tthis.value = value;\n\t\tthis.parent = opts?.parent;\n\t\tthis.children = opts?.children ? [...opts.children] : [];\n\t}\n\n\tadd(value: T, opts?: { index?: number }): TreeNode<T> {\n\t\tconst child = new TreeNode<T>(value, { parent: this });\n\t\tconst idx = Math.min(Math.max(opts?.index ?? this.children.length, 0), this.children.length);\n\t\tthis.children.splice(idx, 0, child);\n\t\treturn child;\n\t}\n}", |
| 12 | + "source": "typescript-algorithms", |
| 13 | + "id": 1, |
| 14 | + "length": 560 |
| 15 | + }, |
| 16 | + { |
| 17 | + "text": "export function binarySearch(nums: number[], target: number): number {\n\tlet low = 0, high = nums.length - 1;\n\twhile (low <= high) {\n\t\tconst mid = (low + high) >> 1;\n\t\tconst value = nums[mid];\n\t\tif (value === target) return mid;\n\t\tif (value < target) low = mid + 1; else high = mid - 1;\n\t}\n\treturn -1; // not found\n}", |
| 18 | + "source": "typescript-algorithms", |
| 19 | + "id": 2, |
| 20 | + "length": 315 |
| 21 | + }, |
| 22 | + { |
| 23 | + "text": "export function quickSort(a: number[], lo = 0, hi = a.length - 1): void {\n\tif (lo >= hi) return; // base case\n\tconst p = partition(a, lo, hi);\n\tquickSort(a, lo, p - 1);\n\tquickSort(a, p + 1, hi);\n}\nfunction partition(a: number[], lo: number, hi: number): number {\n\tconst pivot = a[hi];\n\tlet i = lo;\n\tfor (let j = lo; j < hi; j++) {\n\t\tif (a[j] <= pivot) { [a[i], a[j]] = [a[j], a[i]]; i++; }\n\t}\n\t[a[i], a[hi]] = [a[hi], a[i]];\n\treturn i;\n}", |
| 24 | + "source": "typescript-algorithms", |
| 25 | + "id": 3, |
| 26 | + "length": 437 |
| 27 | + }, |
| 28 | + { |
| 29 | + "text": "export function mergeSort(a: number[]): number[] {\n\tif (a.length <= 1) return a;\n\tconst mid = a.length >> 1;\n\tconst left = mergeSort(a.slice(0, mid));\n\tconst right = mergeSort(a.slice(mid));\n\treturn merge(left, right);\n}\nfunction merge(l: number[], r: number[]): number[] {\n\tconst out: number[] = [];\n\tlet i = 0, j = 0;\n\twhile (i < l.length && j < r.length) {\n\t\tif (l[i] <= r[j]) out.push(l[i++]); else out.push(r[j++]);\n\t}\n\treturn out.concat(l.slice(i)).concat(r.slice(j));\n}", |
| 30 | + "source": "typescript-algorithms", |
| 31 | + "id": 4, |
| 32 | + "length": 476 |
| 33 | + }, |
| 34 | + { |
| 35 | + "text": "export function dijkstra(graph: Record<string, [string, number][]>, start: string) {\n\tconst dist: Record<string, number> = {};\n\tconst seen = new Set<string>();\n\tObject.keys(graph).forEach(v => dist[v] = v === start ? 0 : Infinity);\n\tconst pq: [number, string][] = [[0, start]]; // [distance, node]\n\twhile (pq.length) {\n\t\tpq.sort((a, b) => a[0] - b[0]); // simple PQ for clarity\n\t\tconst [d, u] = pq.shift()!;\n\t\tif (seen.has(u)) continue;\n\t\tseen.add(u);\n\t\tfor (const [v, w] of graph[u] || []) {\n\t\t\tconst nd = d + w;\n\t\t\tif (nd < dist[v]) { dist[v] = nd; pq.push([nd, v]); }\n\t\t}\n\t}\n\treturn dist;\n}", |
| 36 | + "source": "typescript-algorithms", |
| 37 | + "id": 5, |
| 38 | + "length": 593 |
| 39 | + }, |
| 40 | + { |
| 41 | + "text": "export function bfs(adj: Record<string, string[]>, s: string): Record<string, number> {\n\tconst dist: Record<string, number> = { [s]: 0 };\n\tconst q: string[] = [s];\n\tlet head = 0;\n\twhile (head < q.length) {\n\t\tconst u = q[head++];\n\t\tfor (const v of adj[u] || []) {\n\t\t\tif (dist[v] === undefined) {\n\t\t\t\tdist[v] = dist[u] + 1; // tree edge\n\t\t\t\tq.push(v);\n\t\t\t}\n\t\t}\n\t}\n\treturn dist;\n}", |
| 42 | + "source": "typescript-algorithms", |
| 43 | + "id": 6, |
| 44 | + "length": 377 |
| 45 | + }, |
| 46 | + { |
| 47 | + "text": "export function dfs(adj: Record<string, string[]>, s: string): string[] {\n\tconst order: string[] = [];\n\tconst seen = new Set<string>();\n\tfunction visit(u: string) {\n\t\tseen.add(u);\n\t\tfor (const v of adj[u] || []) if (!seen.has(v)) visit(v);\n\t\torder.push(u); // postorder\n\t}\n\tvisit(s);\n\treturn order;\n}", |
| 48 | + "source": "typescript-algorithms", |
| 49 | + "id": 7, |
| 50 | + "length": 300 |
| 51 | + }, |
| 52 | + { |
| 53 | + "text": "export function topoSort(adj: Record<string, string[]>): string[] {\n\tconst indeg: Record<string, number> = {};\n\tfor (const u in adj) { indeg[u] ??= 0; for (const v of adj[u]) indeg[v] = (indeg[v] || 0) + 1; }\n\tconst q: string[] = Object.keys(indeg).filter(k => indeg[k] === 0);\n\tconst order: string[] = [];\n\tlet head = 0;\n\twhile (head < q.length) {\n\t\tconst u = q[head++];\n\t\torder.push(u);\n\t\tfor (const v of adj[u] || []) {\n\t\t\tif (--indeg[v] === 0) q.push(v);\n\t\t}\n\t}\n\treturn order; // if order.length < n, a cycle exists\n}", |
| 54 | + "source": "typescript-algorithms", |
| 55 | + "id": 8, |
| 56 | + "length": 521 |
| 57 | + }, |
| 58 | + { |
| 59 | + "text": "export function kadane(a: number[]): number {\n\tlet best = -Infinity;\n\tlet cur = 0;\n\tfor (const x of a) {\n\t\tcur = Math.max(x, cur + x);\n\t\tbest = Math.max(best, cur);\n\t}\n\treturn best;\n}", |
| 60 | + "source": "typescript-algorithms", |
| 61 | + "id": 9, |
| 62 | + "length": 183 |
| 63 | + }, |
| 64 | + { |
| 65 | + "text": "export class UnionFind {\n\tprivate parent: number[];\n\tprivate rank: number[];\n\tconstructor(n: number) {\n\t\tthis.parent = Array.from({length: n}, (_, i) => i);\n\t\tthis.rank = Array(n).fill(0);\n\t}\n\tfind(x: number): number {\n\t\treturn this.parent[x] === x ? x : (this.parent[x] = this.find(this.parent[x]));\n\t}\n\tunion(a: number, b: number): boolean {\n\t\tlet ra = this.find(a), rb = this.find(b);\n\t\tif (ra === rb) return false;\n\t\tif (this.rank[ra] < this.rank[rb]) [ra, rb] = [rb, ra];\n\t\tthis.parent[rb] = ra;\n\t\tif (this.rank[ra] === this.rank[rb]) this.rank[ra]++;\n\t\treturn true;\n\t}\n}", |
| 66 | + "source": "typescript-algorithms", |
| 67 | + "id": 10, |
| 68 | + "length": 576 |
| 69 | + }, |
| 70 | + { |
| 71 | + "text": "export function fib(n: number): number {\n\tif (n <= 1) return n;\n\tlet a = 0, b = 1;\n\tfor (let i = 2; i <= n; i++) {\n\t\tconst c = a + b;\n\t\ta = b;\n\t\tb = c;\n\t}\n\treturn b;\n}", |
| 72 | + "source": "typescript-algorithms", |
| 73 | + "id": 11, |
| 74 | + "length": 167 |
| 75 | + }, |
| 76 | + { |
| 77 | + "text": "export function heapSort(a: number[]): number[] {\n\tconst n = a.length;\n\tfor (let i = (n >> 1) - 1; i >= 0; i--) heapify(a, n, i); // build max-heap\n\tfor (let end = n - 1; end > 0; end--) {\n\t\t[a[0], a[end]] = [a[end], a[0]]; // move max to end\n\t\theapify(a, end, 0); // restore heap\n\t}\n\treturn a;\n}\nfunction heapify(a: number[], n: number, i: number): void {\n\tlet largest = i;\n\tconst l = 2 * i + 1, r = 2 * i + 2;\n\tif (l < n && a[l] > a[largest]) largest = l;\n\tif (r < n && a[r] > a[largest]) largest = r;\n\tif (largest !== i) { [a[i], a[largest]] = [a[largest], a[i]]; heapify(a, n, largest); }\n}", |
| 78 | + "source": "typescript-algorithms", |
| 79 | + "id": 12, |
| 80 | + "length": 594 |
| 81 | + }, |
| 82 | + { |
| 83 | + "text": "export function countingSort(a: number[], maxVal: number): number[] {\n\tconst count = new Array(maxVal + 1).fill(0);\n\tfor (const x of a) count[x]++; // count occurrences\n\tfor (let i = 1; i < count.length; i++) count[i] += count[i - 1]; // prefix sums\n\tconst out = new Array(a.length);\n\tfor (let i = a.length - 1; i >= 0; i--) {\n\t\tconst x = a[i];\n\t\tout[--count[x]] = x; // stable placement\n\t}\n\treturn out;\n}", |
| 84 | + "source": "typescript-algorithms", |
| 85 | + "id": 13, |
| 86 | + "length": 405 |
| 87 | + }, |
| 88 | + { |
| 89 | + "text": "export function prim(adj: Record<string, [string, number][]>, start: string) {\n\tconst inMST = new Set<string>();\n\tconst parent: Record<string, string | null> = {};\n\tconst key: Record<string, number> = {};\n\tfor (const u in adj) { key[u] = Infinity; parent[u] = null; }\n\tkey[start] = 0;\n\tconst pq: [number, string, string | null][] = [[0, start, null]]; // [key, node, parent]\n\twhile (pq.length) {\n\t\tpq.sort((a, b) => a[0] - b[0]);\n\t\tconst [k, u, p] = pq.shift()!;\n\t\tif (inMST.has(u)) continue;\n\t\tinMST.add(u); parent[u] = p;\n\t\tfor (const [v, w] of adj[u] || []) {\n\t\t\tif (!inMST.has(v) && w < key[v]) { key[v] = w; pq.push([w, v, u]); }\n\t\t}\n\t}\n\treturn { parent, key };\n}", |
| 90 | + "source": "typescript-algorithms", |
| 91 | + "id": 14, |
| 92 | + "length": 668 |
| 93 | + }, |
| 94 | + { |
| 95 | + "text": "export function kruskal(n: number, edges: [number, number, number][]) {\n\tedges.sort((a, b) => a[2] - b[2]);\n\tconst uf = new UF(n);\n\tconst mst: [number, number, number][] = [];\n\tfor (const [u, v, w] of edges) {\n\t\tif (uf.union(u, v)) mst.push([u, v, w]); // add edge if it connects two sets\n\t\tif (mst.length === n - 1) break;\n\t}\n\treturn mst;\n}\nclass UF {\n\tparent: number[];\n\trank: number[];\n\tconstructor(n: number) {\n\t\tthis.parent = Array.from({ length: n }, (_, i) => i);\n\t\tthis.rank = Array(n).fill(0);\n\t}\n\tfind(x: number): number {\n\t\treturn this.parent[x] === x ? x : (this.parent[x] = this.find(this.parent[x]));\n\t}\n\tunion(a: number, b: number): boolean {\n\t\tlet ra = this.find(a), rb = this.find(b);\n\t\tif (ra === rb) return false;\n\t\tif (this.rank[ra] < this.rank[rb]) [ra, rb] = [rb, ra];\n\t\tthis.parent[rb] = ra;\n\t\tif (this.rank[ra] === this.rank[rb]) this.rank[ra]++;\n\t\treturn true;\n\t}\n}", |
| 96 | + "source": "typescript-algorithms", |
| 97 | + "id": 15, |
| 98 | + "length": 890 |
| 99 | + }, |
| 100 | + { |
| 101 | + "text": "export function bellmanFord(n: number, edges: [number,number,number][], src: number) {\n\tconst dist = Array(n).fill(Infinity);\n\tdist[src] = 0;\n\tfor (let i = 0; i < n - 1; i++) {\n\t\tlet updated = false;\n\t\tfor (const [u,v,w] of edges) {\n\t\t\tif (dist[u] + w < dist[v]) { dist[v] = dist[u] + w; updated = true; }\n\t\t}\n\t\tif (!updated) break; // early stop if no changes\n\t}\n\tfor (const [u,v,w] of edges) {\n\t\tif (dist[u] + w < dist[v]) throw new Error('negative cycle');\n\t}\n\treturn dist;\n}", |
| 102 | + "source": "typescript-algorithms", |
| 103 | + "id": 16, |
| 104 | + "length": 478 |
| 105 | + }, |
| 106 | + { |
| 107 | + "text": "export function floydWarshall(dist: number[][]): number[][] {\n\tconst n = dist.length;\n\tconst d = dist.map(row => row.slice()); // copy\n\tfor (let k = 0; k < n; k++) {\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tfor (let j = 0; j < n; j++) {\n\t\t\t\tconst nd = d[i][k] + d[k][j];\n\t\t\t\tif (nd < d[i][j]) d[i][j] = nd;\n\t\t\t}\n\t\t}\n\t}\n\treturn d;\n}", |
| 108 | + "source": "typescript-algorithms", |
| 109 | + "id": 17, |
| 110 | + "length": 325 |
| 111 | + }, |
| 112 | + { |
| 113 | + "text": "export function kmpSearch(text: string, pattern: string): number[] {\n\tif (!pattern) return [];\n\tconst lps = buildLPS(pattern);\n\tconst res: number[] = [];\n\tlet i = 0, j = 0; // i over text, j over pattern\n\twhile (i < text.length) {\n\t\tif (text[i] === pattern[j]) { i++; j++; if (j === pattern.length) { res.push(i - j); j = lps[j-1]; } }\n\t\telse if (j > 0) j = lps[j-1]; else i++;\n\t}\n\treturn res;\n}\nfunction buildLPS(p: string): number[] {\n\tconst lps = Array(p.length).fill(0);\n\tlet len = 0;\n\tfor (let i = 1; i < p.length; ) {\n\t\tif (p[i] === p[len]) lps[i++] = ++len;\n\t\telse if (len) len = lps[len-1];\n\t\telse lps[i++] = 0;\n\t}\n\treturn lps;\n}", |
| 114 | + "source": "typescript-algorithms", |
| 115 | + "id": 18, |
| 116 | + "length": 637 |
| 117 | + }, |
| 118 | + { |
| 119 | + "text": "export function rabinKarp(text: string, pattern: string): number[] {\n\tconst n = text.length, m = pattern.length;\n\tif (!m) return [];\n\tconst base = 911382323, mod = 972663749; // large primes\n\tlet hp = 0, ht = 0, pow = 1;\n\tfor (let i = 0; i < m; i++) {\n\t\thp = (hp * base + pattern.charCodeAt(i)) % mod;\n\t\tht = (ht * base + text.charCodeAt(i)) % mod;\n\t\tif (i < m - 1) pow = (pow * base) % mod;\n\t}\n\tconst res: number[] = [];\n\tfor (let i = 0; i <= n - m; i++) {\n\t\tif (hp === ht && text.slice(i, i + m) === pattern) res.push(i);\n\t\tif (i < n - m) {\n\t\t\tht = ( (ht - text.charCodeAt(i) * pow % mod + mod) % mod );\n\t\t\tht = (ht * base + text.charCodeAt(i + m)) % mod;\n\t\t}\n\t}\n\treturn res;\n}", |
| 120 | + "source": "typescript-algorithms", |
| 121 | + "id": 19, |
| 122 | + "length": 679 |
| 123 | + }, |
| 124 | + { |
| 125 | + "text": "export function quickSelect(a: number[], k: number, lo = 0, hi = a.length - 1): number {\n\twhile (lo <= hi) {\n\t\tconst p = partition(a, lo, hi); // pivot index\n\t\tif (p === k) return a[p];\n\t\tif (p < k) lo = p + 1; else hi = p - 1;\n\t}\n\treturn NaN;\n}\nfunction partition(a: number[], lo: number, hi: number): number {\n\tconst pivot = a[hi];\n\tlet i = lo;\n\tfor (let j = lo; j < hi; j++) {\n\t\tif (a[j] <= pivot) { [a[i], a[j]] = [a[j], a[i]]; i++; }\n\t}\n\t[a[i], a[hi]] = [a[hi], a[i]];\n\treturn i;\n}", |
| 126 | + "source": "typescript-algorithms", |
| 127 | + "id": 20, |
| 128 | + "length": 486 |
| 129 | + }, |
| 130 | + { |
| 131 | + "text": "export class Fenwick {\n\tprivate bit: number[];\n\tconstructor(size: number) { this.bit = new Array(size + 1).fill(0); }\n\tadd(index: number, delta: number): void {\n\t\tfor (let i = index + 1; i < this.bit.length; i += i & -i) this.bit[i] += delta;\n\t}\n\tsum(index: number): number { // prefix sum [0..index]\n\t\tlet res = 0;\n\t\tfor (let i = index + 1; i > 0; i -= i & -i) res += this.bit[i];\n\t\treturn res;\n\t}\n\trangeSum(l: number, r: number): number { // inclusive\n\t\treturn this.sum(r) - (l ? this.sum(l - 1) : 0);\n\t}\n}", |
| 132 | + "source": "typescript-algorithms", |
| 133 | + "id": 21, |
| 134 | + "length": 508 |
| 135 | + } |
| 136 | + ] |
| 137 | +} |
0 commit comments