|
| 1 | +import java.util.ArrayList; |
| 2 | + |
| 3 | +/* |
| 4 | + * @lc app=leetcode id=207 lang=java |
| 5 | + * |
| 6 | + * [207] Course Schedule |
| 7 | + * |
| 8 | + * https://leetcode.com/problems/course-schedule/description/ |
| 9 | + * |
| 10 | + * algorithms |
| 11 | + * Medium (44.27%) |
| 12 | + * Total Accepted: 536.4K |
| 13 | + * |
| 14 | + * There are a total of numCourses courses you have to take, labeled from 0 to |
| 15 | + * numCourses - 1. You are given an array prerequisites where prerequisites[i] |
| 16 | + * = [ai, bi] indicates that you must take course bi first if you want to take |
| 17 | + * course ai. |
| 18 | + * |
| 19 | + * |
| 20 | + * For example, the pair [0, 1], indicates that to take course 0 you have to |
| 21 | + * first take course 1. |
| 22 | + * |
| 23 | + * |
| 24 | + * Return true if you can finish all courses. Otherwise, return false. |
| 25 | + * |
| 26 | + * |
| 27 | + * Example 1: |
| 28 | + * |
| 29 | + * |
| 30 | + * Input: numCourses = 2, prerequisites = [[1,0]] |
| 31 | + * Output: true |
| 32 | + * Explanation: There are a total of 2 courses to take. |
| 33 | + * To take course 1 you should have finished course 0. So it is possible. |
| 34 | + * |
| 35 | + * |
| 36 | + * Example 2: |
| 37 | + * |
| 38 | + * |
| 39 | + * Input: numCourses = 2, prerequisites = [[1,0],[0,1]] |
| 40 | + * Output: false |
| 41 | + * Explanation: There are a total of 2 courses to take. |
| 42 | + * To take course 1 you should have finished course 0, and to take course 0 you |
| 43 | + * should also have finished course 1. So it is impossible. |
| 44 | + * |
| 45 | + * |
| 46 | + * |
| 47 | + * Constraints: |
| 48 | + * |
| 49 | + * |
| 50 | + * 1 <= numCourses <= 10^5 |
| 51 | + * 0 <= prerequisites.length <= 5000 |
| 52 | + * prerequisites[i].length == 2 |
| 53 | + * 0 <= ai, bi < numCourses |
| 54 | + * All the pairs prerequisites[i] are unique. |
| 55 | + * |
| 56 | + * |
| 57 | + */ |
| 58 | + |
| 59 | +public class Solution { |
| 60 | + public boolean canFinish(int numCourses, int[][] prerequisites) { |
| 61 | + // Given infos |
| 62 | + // |
| 63 | + // prerequisites[] is the target class |
| 64 | + // prerequisites[][] are the dependency classes |
| 65 | + |
| 66 | + if (numCourses <= 1) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + ArrayList<Integer>[] graph = new ArrayList[numCourses]; |
| 71 | + // 0 = unvisited |
| 72 | + // 1 = visited (but not all dependency nodes explored) |
| 73 | + // 2 = completed (visited and all dependency nodes explored) |
| 74 | + int[] visited = new int[numCourses]; |
| 75 | + |
| 76 | + for (int i = 0; i < graph.length; i++) { |
| 77 | + graph[i] = new ArrayList<Integer>(); |
| 78 | + } |
| 79 | + |
| 80 | + for (int[] pre : prerequisites) { |
| 81 | + graph[pre[0]].add(pre[1]); |
| 82 | + } |
| 83 | + |
| 84 | + for (int i = 0; i < numCourses; ++i) { |
| 85 | + if (!dfs(graph, i, visited)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + public boolean dfs(ArrayList<Integer>[] graph, int course, int[] visited) { |
| 94 | + // Cycle detected |
| 95 | + if (visited[course] == 1) { |
| 96 | + return false; |
| 97 | + } else if (visited[course] == 2) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + visited[course] = 1; |
| 102 | + |
| 103 | + for (int i = 0; i < graph[course].size(); ++i) { |
| 104 | + if (!dfs(graph, graph[course].get(i), visited)) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + visited[course] = 2; |
| 110 | + return true; |
| 111 | + } |
| 112 | +} |
0 commit comments