Course Schedule IV - Leetcode 1462 - Python

preview_player
Показать описание


0:00 - Read the problem
1:10 - Drawing Explanation
11:52 - Coding Explanation

leetcode 1462

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

I was a little frustrated after trying this problem and failing in the topological sort section :( - I wrote brute force then and got TLE. Either way, this problem is difficult to me if you're trying to find a simple way to implement it with just topological sort in mind.

AustinCS
Автор

"we go to our nother neighbor" @9:29 :) thanks for the explanation

javaluvawithjeremystones
Автор

hey @NeetCodeIO, you ordering pairs in prerequisites from right to left, but in explanation of this task on Leetcode there are direction from left to right, is it mistake of Leetcode platform or yours?

kamila
Автор

class Solution {
Map<Integer, Set<Integer>> set = new HashMap();

public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
List<Boolean> list = new ArrayList();
Map<Integer, Set<Integer>> map = new HashMap();
for (int i=0;i<numCourses;i++) {
map.put(i, new HashSet());
}

for (int[] p : prerequisites)
map.get(p[0]).add(p[1]);

for (int i=0;i<numCourses;i++)
dfs(i, map);

//System.out.println(set);
for (int[] q : queries )
list.add(

return list;
}
Set<Integer> dfs(int i, Map<Integer, Set<Integer>> map) {
if ( !set.containsKey(i) ) {
Set<Integer> temp = new HashSet();

for ( int neigh : map.get(i))
temp.addAll( dfs(neigh, map) );


temp.add(i);
set.put(i, temp);
}



return set.get(i);
}

}

yang
Автор

I find Kahn's Algorithm and BFS approaches a lot more straight forward for these kind of problems.

fishtank-
Автор

Is there a method to which new problems you upload? Or do you just upload whatever problem you’re practicing at the time?

chair_smesh
Автор

Solved if by myself after understanding the concept from course schedule 2, Thanks NC !! 😅

pushpavathikn
Автор

Once you use hashmap to store what nodes that current node can reach, time complexity of running DFS can be O(N+E) instead O(N(N+E))?

ray
Автор

What's the space complexity? Is it the same as the time complexity?

monicawang
Автор

I have been following these course schedule problems but one thing i didn't understand is how is the time complexity O(P+N)?? Can someone explain??

cooltomcatty
Автор

Where is Course Schedule III? can you please do one soon :)

piglovesasy
Автор

Hey Neet,
can you make video on how to stay ahead with rise of AI as a programmer?

cosepeter
Автор

Hey NeetCode … you know what? You are the best…

sambro
Автор

Help: looking for solution of leetcode 1203:

cocodinary
visit shbcf.ru