code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

02 Course Schedule 2

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const courseSchedule2 = (numCourses, prerequisites) => {
  let adj = {}
  for (let i = 0; i < numCourses; i++) {
    adj[i] = []
  }
  for (let [v, e] of prerequisites) {
    adj[v].push(e)
  }
  let cycle = new Set()
  let visited = new Set()
  let output = []
  const dfs = node => {
    if (cycle.has(node)) {
      return false
    }
    if (visited.has(node)) {
      return true
    }
    cycle.add(node)
    for (const v of adj[node]) {
      if (!dfs(v)) {
        return false
      }
    }
    cycle.delete(node)
    visited.add(node)
    output.push(node)
    return true
  }
  for (let i = 0; i < numCourses; i++) {
    if (!dfs(i)) {
      return []
    }
  }
  return output
}

console.log(courseSchedule2(2, [[1, 0]]))

Created 2022-02-10T05:45:44+00:00 · Edit