In what order should you study? Topological sort answers that. 📚 (LeetCode 207 / 210)
In what order should you study? Topological sort answers that. 📚 (LeetCode 207 / 210)
Arrow A → B = take A before B
Kahn's algorithm:
1️⃣ Count prerequisites for every course (in-degree)
2️⃣ Put every course with 0 into a queue
3️⃣ Take one → it's next in your order → every course it points to drops by 1
4️⃣ Hits 0? Into the queue. Repeat.
Result: Java → SQL → DSA → OOP → Spring → Interviews
Got all 6? Valid order, no cycle ✅
Got fewer? There's a cycle → impossible (LeetCode 207 returns false)
Time O(V + E)
Save this for your next DSA interview 💾
#leetcode #dsa #java #codinginterview #graphs
Comments
Comments from the original post on Instagram
No comments yet
