Spaces:
Running
Running
File size: 4,190 Bytes
8800c81 2052d94 8800c81 2052d94 8800c81 2052d94 8800c81 59a8d26 8800c81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import { NEETCODE_150_DATA, type CatalogProblem } from '../data/neetcode150Data';
export type { CatalogProblem };
// Pre-built index maps for instant O(1) lookups
const problemNumberMap = new Map<number, CatalogProblem>();
const problemIdMap = new Map<string, CatalogProblem>();
const patternMap = new Map<string, CatalogProblem[]>();
for (const prob of NEETCODE_150_DATA) {
problemNumberMap.set(prob.problem_number, prob);
problemIdMap.set(prob.id, prob);
const patternList = patternMap.get(prob.pattern) || [];
patternList.push(prob);
patternMap.set(prob.pattern, patternList);
}
/**
* Returns a catalog problem by its official LeetCode problem number.
*/
export function getCatalogProblemByNumber(problemNumber: number | string | undefined | null): CatalogProblem | undefined {
if (problemNumber === undefined || problemNumber === null) return undefined;
const num = typeof problemNumber === 'string' ? parseInt(problemNumber, 10) : problemNumber;
if (isNaN(num)) return undefined;
return problemNumberMap.get(num);
}
/**
* Returns a catalog problem by its catalog ID (e.g. "nc-217", "prob-contains-duplicate", or "217").
*/
export function getCatalogProblemById(id: string | undefined | null): CatalogProblem | undefined {
if (!id) return undefined;
const direct = problemIdMap.get(id);
if (direct) return direct;
// If numeric ID string (e.g. "217"), check problemNumberMap
if (/^\d+$/.test(id)) {
return problemNumberMap.get(parseInt(id, 10));
}
// Normalize slug / id format (e.g. "prob-contains-duplicate" -> "containsduplicate")
const cleanId = id.replace(/^prob-/, '').toLowerCase().replace(/[^a-z0-9]+/g, '');
for (const prob of NEETCODE_150_DATA) {
const probNormalized = prob.name.toLowerCase().replace(/[^a-z0-9]+/g, '');
if (probNormalized === cleanId || prob.id.toLowerCase().replace(/[^a-z0-9]+/g, '') === cleanId) {
return prob;
}
}
return undefined;
}
/**
* Returns a catalog problem by its problem name/title.
*/
export function getCatalogProblemByName(name: string | undefined | null): CatalogProblem | undefined {
if (!name) return undefined;
const cleanName = name.toLowerCase().replace(/\s*-\s*method.*$/i, '').replace(/[^a-z0-9]+/g, '');
for (const prob of NEETCODE_150_DATA) {
const probNormalized = prob.name.toLowerCase().replace(/[^a-z0-9]+/g, '');
if (probNormalized === cleanName) {
return prob;
}
}
return undefined;
}
/**
* Robust, unified catalog lookup across problem number, ID, and problem title.
*/
export function findCatalogProblem(
problemNumber?: number | string | null,
id?: string | null,
name?: string | null
): CatalogProblem | undefined {
if (problemNumber !== undefined && problemNumber !== null) {
const p = getCatalogProblemByNumber(problemNumber);
if (p) return p;
}
if (id) {
const p = getCatalogProblemById(id);
if (p) return p;
}
if (name) {
const p = getCatalogProblemByName(name);
if (p) return p;
}
return undefined;
}
/**
* Returns all 150 curated problems in canonical expedition sequence.
*/
export function getAllCatalogProblems(): CatalogProblem[] {
return NEETCODE_150_DATA;
}
/**
* Returns all problems belonging to a specific algorithm pattern.
*/
export function getCatalogProblemsByPattern(pattern: string): CatalogProblem[] {
return patternMap.get(pattern) || [];
}
/**
* Returns all unique pattern categories across the 150 problems.
*/
export function getAllPatterns(): string[] {
return Array.from(patternMap.keys());
}
/**
* Finds the next unsolved problem in the trail sequence given a set of completed numbers.
*/
export function getNextUnsolvedProblem(
completedNumbers: Set<number>,
sequence?: number[]
): CatalogProblem | undefined {
if (sequence && sequence.length > 0) {
for (const num of sequence) {
if (!completedNumbers.has(num)) {
const prob = problemNumberMap.get(num);
if (prob) return prob;
}
}
}
// Fallback to iterating canonical catalog
for (const prob of NEETCODE_150_DATA) {
if (!completedNumbers.has(prob.problem_number)) {
return prob;
}
}
return undefined;
}
|