Class Solution

java.lang.Object
g0201_0300.s0207_course_schedule.Solution

public class Solution extends Object
207 - Course Schedule.<p>Medium</p> <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.</p> <ul> <li>For example, the pair <code>[0, 1]</code>, indicates that to take course <code>0</code> you have to first take course <code>1</code>.</li> </ul> <p>Return <code>true</code> if you can finish all courses. Otherwise, return <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> numCourses = 2, prerequisites = [[1,0],[0,1]]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= numCourses <= 10<sup>5</sup></code></li> <li><code>0 <= prerequisites.length <= 5000</code></li> <li><code>prerequisites[i].length == 2</code></li> <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code></li> <li>All the pairs prerequisites[i] are <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • canFinish

      public boolean canFinish(int numCourses, int[][] prerequisites)