{"id":22513859,"url":"https://github.com/emahtab/course-schedule-ii","last_synced_at":"2026-02-27T05:01:02.129Z","repository":{"id":79525398,"uuid":"262571470","full_name":"eMahtab/course-schedule-ii","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-15T04:15:04.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-09T19:04:14.068Z","etag":null,"topics":["leetcode","problem-solving","queue","topological-sort"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-09T12:59:10.000Z","updated_at":"2025-01-15T04:15:06.000Z","dependencies_parsed_at":"2025-02-02T03:25:29.520Z","dependency_job_id":"3950c156-b4d1-4515-8941-0072d5bf1e59","html_url":"https://github.com/eMahtab/course-schedule-ii","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/course-schedule-ii","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcourse-schedule-ii","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcourse-schedule-ii/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcourse-schedule-ii/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcourse-schedule-ii/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/course-schedule-ii/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcourse-schedule-ii/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29885799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T23:51:21.483Z","status":"online","status_checked_at":"2026-02-27T02:00:06.759Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","problem-solving","queue","topological-sort"],"created_at":"2024-12-07T03:14:44.466Z","updated_at":"2026-02-27T05:01:02.110Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Course Schedule II\n## https://leetcode.com/problems/course-schedule-ii\n\nThere are a total of n courses you have to take, labeled from 0 to n-1.\n\nSome courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]\n\nGiven the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.\n\nThere may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.\n```\nExample 1:\n\nInput: 2, [[1,0]] \nOutput: [0,1]\nExplanation: There are a total of 2 courses to take. To take course 1 you should have finished   \n             course 0. So the correct course order is [0,1] .\nExample 2:\n\nInput: 4, [[1,0],[2,0],[3,1],[3,2]]\nOutput: [0,1,2,3] or [0,2,1,3]\nExplanation: There are a total of 4 courses to take. To take course 3 you should have finished both     \n             courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. \n             So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .\n```\n**Note:**\n\n1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.\n2. You may assume that there are no duplicate edges in the input prerequisites.\n\n# Implementation 1 : BFS (without creating a graph representation)\n\nn = number of courses (numCourses)\n\nm = prerequisites.length\n\n**Runtime = O(n * m)**\n\n**Space = O(n)**\n\n```java\nclass Solution {\n    public int[] findOrder(int numCourses, int[][] prerequisites) {\n        int[] indegree = new int[numCourses];\n        for(int[] prereq : prerequisites) {\n            indegree[prereq[0]]++;\n        }\n        Queue\u003cInteger\u003e q = new ArrayDeque\u003c\u003e();\n        for(int i = 0; i \u003c indegree.length; i++) {\n            if(indegree[i] == 0)\n                q.add(i);\n        }\n        int courseCompleted = 0;\n        int order[] = new int[numCourses];\n        int index = 0;\n        while(!q.isEmpty()) {\n            courseCompleted++;\n            int course = q.poll();\n            order[index++] = course;\n            for(int i = 0; i \u003c prerequisites.length; i++) {\n                int[] prereq = prerequisites[i];\n                if(prereq[1] == course) {\n                    indegree[prereq[0]]--;\n                    if(indegree[prereq[0]] == 0) {\n                    q.add(prereq[0]);\n                   }\n                }\n            }\n        }\n        return index == numCourses ? order : new int[0];\n    }\n}\n```\n# Implementation 2 (creating adjacency list with arraylist) : BFS (by creating a graph representation)\n\nn = number of courses (numCourses)\n\nm = prerequisites.length\n\n**Runtime = O(n + m)**\n\n**Space = O(n + m)**\n\n```java\nclass Solution {\n    public int[] findOrder(int numCourses, int[][] prerequisites) {\n        int[] indegree = new int[numCourses];\n        Map\u003cInteger, Set\u003cInteger\u003e\u003e graph = new HashMap\u003c\u003e();\n        int[] topologicalOrder = new int[numCourses];\n        \n        // Create the adjacency list representation of the graph\n        for (int i = 0; i \u003c prerequisites.length; i++) {\n          int dest = prerequisites[i][0];\n          indegree[dest]++;\n            \n          int src = prerequisites[i][1];\n          Set\u003cInteger\u003e neighbors = graph.getOrDefault(src, new HashSet\u003cInteger\u003e());\n          neighbors.add(dest);\n            \n          graph.put(src, neighbors);\n        }\n        \n        // Add all vertices with 0 in-degree to the queue\n        Queue\u003cInteger\u003e q = new LinkedList\u003c\u003e();\n        for (int i = 0; i \u003c indegree.length; i++) {\n          if (indegree[i] == 0) \n             q.add(i);\n        }\n        \n        int i = 0;\n        while (!q.isEmpty()) {\n          int node = q.remove();\n          topologicalOrder[i++] = node;\n          // Decrement the in-degree of each neighbor of node by 1\n          if (graph.containsKey(node)) {\n            for(Integer neighbor : graph.get(node)) {\n                 indegree[neighbor]--;\n\n              // If in-degree of a neighbor becomes 0, add it to the queue\n              if (indegree[neighbor] == 0) {\n                q.add(neighbor);\n              }\n            }\n          }\n        }\n        \n        return i == numCourses ? topologicalOrder : new int[0];\n    }\n}\n```\n\n## Implementation 2a (creating adjacency list with arraylist) : BFS with Adjacency list\n\nn = number of courses (numCourses)\n\nm = prerequisites.length\n\n**Runtime = O(n + m)**\n\n**Space = O(n + m)**\n\n```java\nclass Solution {\n    public int[] findOrder(int numCourses, int[][] prerequisites) {\n        List\u003cList\u003cInteger\u003e\u003e adjacencyList = new ArrayList\u003c\u003e();\n        int[] indegree = new int[numCourses];\n        Queue\u003cInteger\u003e queue = new ArrayDeque\u003c\u003e();\n        int courseCompleted = 0;\n        int[] result = new int[numCourses];\n        int index = 0;\n\n        for(int i = 0; i \u003c numCourses; i++)\n            adjacencyList.add(new ArrayList\u003c\u003e());\n\n        for(int[] prerequisite : prerequisites) {\n            int a = prerequisite[0];\n            int b = prerequisite[1];\n            indegree[a]++;\n            adjacencyList.get(b).add(a);\n        }\n        for(int i = 0; i \u003c numCourses; i++){\n            if(indegree[i] == 0)\n              queue.offer(i);\n        }\n\n        while(!queue.isEmpty()) {\n            int course = queue.remove();\n            courseCompleted++;\n            result[index++] = course;\n            for(int neighbor : adjacencyList.get(course)){\n                indegree[neighbor]--;\n                if(indegree[neighbor] == 0)\n                  queue.offer(neighbor);\n            }\n        }\n\n       return courseCompleted == numCourses ? result : new int[0]; \n    }\n}\n```\n\n## Observations :\n1. We can use either Stack or Queue to solve this problem.\n\n2. One important thing to note is, using a Queue may produce a different order of course completion, than the order produced by using a Stack.\nThis is because the way Queue works is different than the Stack. Queue is FIFO (First in First out) while Stack is LIFO (Last in First out).\n\n\n# References :\n1. https://leetcode.com/articles/course-schedule-ii\n2. https://www.youtube.com/watch?v=TJkYn3oqX0k\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcourse-schedule-ii","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcourse-schedule-ii","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcourse-schedule-ii/lists"}