From 28fdd13ec6583281002102108476af755fb43fe0 Mon Sep 17 00:00:00 2001 From: Harshita Patidar Date: Sat, 4 Oct 2025 13:55:57 +0530 Subject: [PATCH 1/2] Create 207. Course Schedule.cpp --- 207. Course Schedule.cpp | 102 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 207. Course Schedule.cpp diff --git a/207. Course Schedule.cpp b/207. Course Schedule.cpp new file mode 100644 index 0000000..f2cf1e9 --- /dev/null +++ b/207. Course Schedule.cpp @@ -0,0 +1,102 @@ +/* +207. Course Schedule +Medium + +There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. +You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that +you must take course bi first if you want to take course ai. + +For example: +- [0, 1] means: to take course 0 you must finish course 1 first. + +Return true if you can finish all courses. Otherwise, return false. + + +---------------- Example 1 ---------------- +Input: numCourses = 2, prerequisites = [[1,0]] +Output: true +Explanation: To take course 1 you must finish course 0 → possible. + +---------------- Example 2 ---------------- +Input: numCourses = 2, prerequisites = [[1,0],[0,1]] +Output: false +Explanation: Cycle exists (0 requires 1 and 1 requires 0) → impossible. + + +Constraints: +1 <= numCourses <= 2000 +0 <= prerequisites.length <= 5000 +prerequisites[i].length == 2 +0 <= ai, bi < numCourses +All pairs prerequisites[i] are unique. +*/ + + +// ---------------- Approach ---------------- +// +// This is a **cycle detection in a directed graph** problem. +// - Each course is a node. +// - A prerequisite pair [a, b] represents a directed edge b → a. +// - If there is a cycle in this graph, it means courses are interdependent +// and cannot all be finished. +// +// We use DFS with two arrays: +// - visited[node]: to mark nodes that have been fully processed. +// - pathVisit[node]: to mark nodes in the current DFS recursion stack. +// +// If during DFS we encounter a node that is already in the current path (pathVisit), +// it means we found a cycle → return false. + + +// ---------------- Intuition ---------------- +// +// - Think of prerequisites as a graph of dependencies. +// - If a cycle exists, some course depends on itself indirectly → impossible to finish. +// - If no cycle exists, we can always complete courses in a valid topological order. + + +// ---------------- Solution in Code ---------------- + +class Solution { +public: + bool dfs(vector> &adj, vector &visited, vector &pathVisit, int node) { + visited[node] = 1; + pathVisit[node] = 1; + + for (int neighbor : adj[node]) { + if (!visited[neighbor]) { + if (dfs(adj, visited, pathVisit, neighbor)) { + return true; // cycle detected + } + } + else if (pathVisit[neighbor]) { + return true; // back edge → cycle + } + } + + pathVisit[node] = 0; // backtrack + return false; + } + + bool canFinish(int numCourses, vector>& prerequisites) { + // Build adjacency list from prerequisites + vector> adj(numCourses); + for (auto &edge : prerequisites) { + int a = edge[0], b = edge[1]; + adj[b].push_back(a); + } + + vector visited(numCourses, 0); + vector pathVisit(numCourses, 0); + + // Run DFS from each unvisited node + for (int i = 0; i < numCourses; i++) { + if (!visited[i]) { + if (dfs(adj, visited, pathVisit, i)) { + return false; // cycle found + } + } + } + return true; + } +}; From 68aa1bda63ff50400cae7ea15edbcfd8c34c117f Mon Sep 17 00:00:00 2001 From: SjxSubham Date: Sat, 4 Oct 2025 22:42:08 +0530 Subject: [PATCH 2/2] Remove problem description and examples from Course Schedule Removed problem statement and example comments from the Course Schedule file. --- 207. Course Schedule.cpp | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/207. Course Schedule.cpp b/207. Course Schedule.cpp index f2cf1e9..bd1fe4a 100644 --- a/207. Course Schedule.cpp +++ b/207. Course Schedule.cpp @@ -1,37 +1,3 @@ -/* -207. Course Schedule -Medium - -There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. -You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that -you must take course bi first if you want to take course ai. - -For example: -- [0, 1] means: to take course 0 you must finish course 1 first. - -Return true if you can finish all courses. Otherwise, return false. - - ----------------- Example 1 ---------------- -Input: numCourses = 2, prerequisites = [[1,0]] -Output: true -Explanation: To take course 1 you must finish course 0 → possible. - ----------------- Example 2 ---------------- -Input: numCourses = 2, prerequisites = [[1,0],[0,1]] -Output: false -Explanation: Cycle exists (0 requires 1 and 1 requires 0) → impossible. - - -Constraints: -1 <= numCourses <= 2000 -0 <= prerequisites.length <= 5000 -prerequisites[i].length == 2 -0 <= ai, bi < numCourses -All pairs prerequisites[i] are unique. -*/ - - // ---------------- Approach ---------------- // // This is a **cycle detection in a directed graph** problem.