Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 823 Bytes

course_schedule.md

File metadata and controls

41 lines (33 loc) · 823 Bytes

Topological Sort

import 'dart:collection';

class Solution {
  bool canFinish(int numCourses, List<List<int>> prerequisites) {
    final List<int> answer = [];
    final Map<int, List<int>> map = Map();
    final List<int> result = List<int>.filled(numCourses, 0);

    for (final List<int> x in prerequisites) {
      map[x[1]] = [...map[x[1]] ?? [], x[0]];
      result[x[0]]++;
    }

    final Queue<int> q = Queue<int>();
    for (int i = 0; i < numCourses; i++) {
      if (result[i] == 0) {
        q.add(i);
      }
    }

    while (q.isNotEmpty) {
      final int fr = q.removeFirst();
      answer.add(fr);

      for (final int x in map[fr] ?? []) {
        result[x]--;
        if (result[x] == 0) {
          q.add(x);
        }
      }
    }

    return answer.length == numCourses;
  }
}