Question 1 Course Schedule

Step 1: Model the Question

  • this is a Graph problem

Step 2: Let us build the Graph together

  • Node: courses

  • Edge: prerequisites ==》 前置课关系

Step 3: Define direction

Let me define the dependency direction first:

  • computer science: a --> b, data strucutre --> algorithm means b depends a

  • based on the question: some courses may have prerequisites,

    • for example to take course 0 you have to first take course 1, which is expressed as a pair [0,1], 1--> 0

  • Ok, so this is actually a topological sorting problem in the graph I built.

    • The question is aksing me to return valid toplological sort/ exist a valid topological sort or not

Step 4: Detail Logic

Step 5: 时间空间复杂度

  • TC: O(|V| + |E|)

  • SC: O(V)

Step 6: Implementation

Last updated