'Course Schedule' Problem .Can you help me optimize my code

problem link : https://leetcode.com/problems/course-schedule/

code link : https://ide.codingblocks.com/s/284983

I am using the cycle detection in directed graph approach, but I am getting a TLE . Can you help me optimize my code

this can be done using topological sort
method
u can read the discuss top voted solution

the approach I am using is very similar to topological sort, in fact topoligical sort is little modification in dfs. Can you suggest me any optimization. Majority of the people there have used cycle detection approach there also .

@amitqy I’ll see through your code.
Till then I have a simple solution with topo sort with bfs .

You’re sending a copy of the adjacency list each time. It takes a lot of time to copy the content to a new list. Use vector<vector> &adjacenyList for saving time. It would optimise a lot.

2 Likes

ya it worked like a charm! thanks !

@amitqy No problem dude.