Skip to content

Commit

Permalink
fix course schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslaw-weber committed May 21, 2024
1 parent 0f285ed commit 95e9f70
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
31 changes: 24 additions & 7 deletions src/problem/list/course-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface CourseScheduleInput {
}
interface LogExtraInfo {
current?: number;
prerequisitesIndex?: number;
allCoursesTaken?: boolean;
inDegreeIndex?: number;
prev?: number[];
Expand All @@ -44,7 +45,7 @@ export function courseSchedule(p: CourseScheduleInput): ProblemState[] {

// Log function to capture steps
function log(stepPoint: number, extraInfo: LogExtraInfo) {
let values: any = { };
let values: any = {};

const variables: Variable[] = [];
const {
Expand All @@ -56,6 +57,7 @@ export function courseSchedule(p: CourseScheduleInput): ProblemState[] {
inDegreeIndex,
prevIndex,
count,
prerequisitesIndex,
} = extraInfo;
values = { ...values };

Expand All @@ -65,10 +67,15 @@ export function courseSchedule(p: CourseScheduleInput): ProblemState[] {
if (neighbor) {
values.neighbor = neighbor;
}
const m2 = from2dArrayToMap(prerequisites)
variables.push(asHashmap("prerequisites", m2, {value:"", color: "neutral"}),)
const m2 = from2dArrayToMap(prerequisites);
variables.push(
asHashmap("prerequisites", m2, {
value: prerequisitesIndex,
color: "primary",
})
);
const m = from2dArrayToMap(graph);
variables.push(...asSimpleValue({numCourses}))
variables.push(...asSimpleValue({ numCourses }));
console.log("graph", m);
variables.push(
asArray("inDegree", inDegree, inDegreeIndex),
Expand All @@ -77,7 +84,7 @@ export function courseSchedule(p: CourseScheduleInput): ProblemState[] {
variables.push(
asHashmap("graph", m, {
value: graphRow,
color: "neutral",
color: "primary",
})
);

Expand Down Expand Up @@ -105,10 +112,20 @@ export function courseSchedule(p: CourseScheduleInput): ProblemState[] {

// Initialize the graph and in-degree array
prerequisites.forEach(([course, prereq]) => {
log(2, { course, prereq, inDegreeIndex: course });
log(2, {
course,
prereq,
inDegreeIndex: course,
prerequisitesIndex: course,
});
graph[prereq].push(course);
inDegree[course]++;
log(3, { course, prereq, inDegreeIndex: course });
log(3, {
course,
prereq,
inDegreeIndex: course,
prerequisitesIndex: course,
});
});

// Log after initialization
Expand Down
2 changes: 1 addition & 1 deletion src/problem/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export function from2dArrayToMap(arr: any[][]): Map<any, any> {
const result = new Map();
for (const row of arr) {
if (row.length) {
result.set(row[0].toString(), row.slice(1).map(x=>x.toString()).join(","));
result.set(row[0], row.slice(1).map(x=>x.toString()).join(","));
}
}
return result;
Expand Down

0 comments on commit 95e9f70

Please sign in to comment.