DEV Community

Cover image for Day 31 of 100 days dsa coding challenge
Manasi Patil
Manasi Patil

Posted on

Day 31 of 100 days dsa coding challenge

Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! πŸ’»πŸ”₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! πŸš€

100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney

Problem:

https://www.geeksforgeeks.org/problems/course-schedule/1

Course Schedule II

Difficulty: Medium Accuracy: 51.77%

You are given n courses, labeled from 0 to n - 1 and a 2d array prerequisites[][] where prerequisites[i] = [x, y] indicates that we need to take course y first if we want to take course x.
Find the ordering of courses we should take to complete all the courses.
Note: There may be multiple correct orders, you just need to return any one of them. If it is impossible to finish all tasks, return an empty array. The Driver code will print true if you return any correct order of courses else it will print false.

Examples:
Input: n = 3, prerequisites[][] = [[1, 0], [2, 1]]
Output: true
Explanation: To take course 1, you must finish course 0. To take course 2, you must finish course 1. So the only valid order is [0, 1, 2].
Input: n = 4, prerequisites[][] = [[2, 0], [2, 1], [3, 2]]
Output: true
Explanation: Course 2 requires both 0 and 1. Course 3 requires course 2. Hence, both [0, 1, 2, 3] and [1, 0, 2, 3] are valid.

Constraints:
1 ≀ n ≀ 104
0 ≀ prerequisites.size() ≀ 105
0 ≀ prerequisites[i][0], prerequisites[i][1] < n
All prerequisite pairs are unique
prerequisites[i][0] β‰  prerequisites[i][1]

Solution:
class Solution:
def findOrder(self, n, prerequisites):
adj = [[] for _ in range(n)]
indegree = [0] * n
for a, b in prerequisites:
adj[b].append(a)
indegree[a] += 1
q = []
for i in range(n):
if indegree[i] == 0:
q.append(i)
res = []
while q:
node = q.pop(0)
res.append(node)
for nei in adj[node]:
indegree[nei] -= 1
if indegree[nei] == 0:
q.append(nei)
return res if len(res) == n else []

Top comments (0)