Course Schedule IV - Leetcode 1462 - Python

  Рет қаралды 9,797

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 28
@okoyemoses9019
@okoyemoses9019 21 күн бұрын
I find Kahn's Algorithm and BFS approaches a lot more straight forward for these kind of problems.
@pushpavathikn8314
@pushpavathikn8314 Ай бұрын
Solved if by myself after understanding the concept from course schedule 2, Thanks NC !! 😅
@sergiofranklin8809
@sergiofranklin8809 Ай бұрын
can you write your solution? I want to see how it's related with course_schedule 2
@pushpavathikn8314
@pushpavathikn8314 Ай бұрын
@ ignore the print statements class Solution: def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]: preq = {i:[] for i in range(numCourses)} ans = [] indirect_preq = {i: set() for i in range(numCourses)} for pre_req, crs in prerequisites: preq[crs].append(pre_req) visit = set() def dfs(crs): if preq[crs] == []: indirect_preq[crs].add(crs) return indirect_preq[crs] if crs in visit: return indirect_preq[crs] for c in preq[crs]: reqs = dfs(c) indirect_preq[crs].update(reqs) visit.add(crs) indirect_preq[crs].add(crs) return indirect_preq[crs] for c in range(numCourses): dfs(c) print(indirect_preq.keys()) print(indirect_preq.values()) for q in queries: if q[0] in indirect_preq.get(q[1]): ans.append(True) else: ans.append(False) return ans
@pushpavathikn8314
@pushpavathikn8314 Ай бұрын
class Solution: def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]: preq = {i:[] for i in range(numCourses)} ans = [] indirect_preq = {i: set() for i in range(numCourses)} for pre_req, crs in prerequisites: preq[crs].append(pre_req) visit = set() def dfs(crs): if preq[crs] == []: indirect_preq[crs].add(crs) return indirect_preq[crs] if crs in visit: return indirect_preq[crs] for c in preq[crs]: reqs = dfs(c) indirect_preq[crs].update(reqs) visit.add(crs) indirect_preq[crs].add(crs) return indirect_preq[crs] for c in range(numCourses): dfs(c) print(indirect_preq.keys()) print(indirect_preq.values()) for q in queries: if q[0] in indirect_preq.get(q[1]): ans.append(True) else: ans.append(False) return ans
@kamila5207
@kamila5207 Жыл бұрын
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?
@paras9392
@paras9392 10 ай бұрын
Here neetcode's explanation A -> B does not indicate that A is a prerequisite of B (like what this notation meant e.g. in course schedule I and II problem) instead the arrow here indicates all the nodes which are (direct) prerequisites of A. So here we are analyzing the inverted relationship i.e. instead of looking for all the nodes which are prerequisite of a node, we are looking for all the direct prerequisite nodes of a particular node .
@yang5843
@yang5843 Жыл бұрын
class Solution { Map set = new HashMap(); public List checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) { List list = new ArrayList(); Map map = new HashMap(); for (int i=0;i
@chair_smesh
@chair_smesh Жыл бұрын
Is there a method to which new problems you upload? Or do you just upload whatever problem you’re practicing at the time?
@shavvasatya7131
@shavvasatya7131 Жыл бұрын
Daily Leetcode Problems.
@yang5843
@yang5843 Жыл бұрын
Today's problem can be found here: kzbin.info/www/bejne/gV61iq16j9Cteas
@ray811030
@ray811030 Жыл бұрын
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))?
@sambro890
@sambro890 Жыл бұрын
Hey NeetCode … you know what? You are the best…
@sonicjetson6253
@sonicjetson6253 Жыл бұрын
chaatu
@iamnoob7593
@iamnoob7593 Ай бұрын
thanks man
@cooltomcatty
@cooltomcatty Жыл бұрын
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??
@theJasin
@theJasin Жыл бұрын
You’re only visiting the nodes and edges once which makes it O(V+E)
@monicawang8447
@monicawang8447 5 ай бұрын
What's the space complexity? Is it the same as the time complexity?
@cosepeter2197
@cosepeter2197 Жыл бұрын
Hey Neet, can you make video on how to stay ahead with rise of AI as a programmer?
@theJasin
@theJasin Жыл бұрын
I think Kahns is more straightforward
@cocodinary
@cocodinary 6 ай бұрын
Help: looking for solution of leetcode 1203: sort-items-by-groups-respecting-dependencies
@piglovesasy
@piglovesasy 3 ай бұрын
Where is Course Schedule III? can you please do one soon :)
@aaditya_87
@aaditya_87 8 ай бұрын
can we use union find ?
@bluesteel1
@bluesteel1 7 ай бұрын
You cannot use union find in directed graphs
@wallyyu5426
@wallyyu5426 10 ай бұрын
made it so easy
@wallyyu5426
@wallyyu5426 10 ай бұрын
interviewing amazon intern tomorrow, this video will help me get that offer!
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 190 М.
КОНЦЕРТЫ:  2 сезон | 1 выпуск | Камызяки
46:36
ТНТ Смотри еще!
Рет қаралды 3,7 МЛН
I'VE MADE A CUTE FLYING LOLLIPOP FOR MY KID #SHORTS
0:48
A Plus School
Рет қаралды 20 МЛН
Wednesday VS Enid: Who is The Best Mommy? #shorts
0:14
Troom Oki Toki
Рет қаралды 50 МЛН
G-24. Course Schedule I and II | Pre-requisite Tasks | Topological Sort
11:32
The Only Database Abstraction You Need | Prime Reacts
21:42
ThePrimeTime
Рет қаралды 232 М.
How to Solve ANY Time/Schedule/Calendar Problem on Leetcode
19:08
babybear4812
Рет қаралды 4,1 М.
Longest Turbulent Array - Leetcode 978 - Python
13:14
NeetCodeIO
Рет қаралды 14 М.
Path with Maximum Probability - Leetcode 1514 - Python
13:11
NeetCodeIO
Рет қаралды 15 М.
Course Schedule - Graph Adjacency List - Leetcode 207
15:47
NeetCode
Рет қаралды 388 М.
Student Attendance Record II - Leetcode 552 - Python
27:10
NeetCodeIO
Рет қаралды 10 М.
Evaluate Division - Leetcode 399 - Python
17:37
NeetCodeIO
Рет қаралды 34 М.