algospaced-dsa / algospaced-ui /src /services /catalogService.ts
iamfebin's picture
feat: multi-solution active recall, dual solution views, and specification panel redesign
59a8d26
Raw History Blame Contribute Delete
4.19 kB
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;
}