Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions course.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Time Complexity : O(V+E) where v is number of course and E is number of dependencies
# Space complexity :O(V+E)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : converting the logic of mapping the dependency of each course was hard it took time.

# Your code here along with comments explaining your approach
# Create an adjacency list linking prerequisites to their dependent courses and counting exactly how many prerequisites each course requires.
# Scan the indegree array to find all courses that require no prerequisites and place them into a queue to process first.
# Process courses from the queue and reduce the indegree of any dependent courses by 1, and add them to the queue. If all courses processed then return True else False.

class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
indegree = [0] * numCourses
graph = {}

#mapping dependency of each course
for pre in prerequisites:
indegree[pre[0]] += 1
if pre[1] not in graph:
graph[pre[1]] = []
graph[pre[1]].append(pre[0])

count = 0
q = deque()

#add independent course to queue
for i in range(0,numCourses):
if indegree[i] == 0:
q.append(i)
count += 1

if not q:
return False
if count == numCourses:
return True

# updating the q of dependency
while q:
curr = q.popleft()
dependency = graph.get(curr)
if dependency:
for course in dependency:
indegree[course] -= 1
if indegree[course] == 0:
q.append(course)
count += 1
if count == numCourses:
return True
return False
41 changes: 41 additions & 0 deletions level_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Time Complexity : O(n)
# Space complexity :O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : Got confused while implementing call and storing list for each level.

# Your code here along with comments explaining your approach
# Creating a queue starting with the root node to keep track of nodes to visit.
# Loop through the queue recording its current size to know exactly how many nodes belong to the current level.
# Pop each node for that level from the front, save its value, and add its left and right children to the back of the queue to be processed in the next level.

from collections import deque
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def levelOrder(self, root):
"""
:type root: Optional[TreeNode]
:rtype: List[List[int]]
"""
result = []

if root is None:
return []

q = deque([root])
while q:
size = len(q)
temp = []
for i in range(0,size):
curr = q.popleft()
temp.append(curr.val)
if curr.left:
q.append(curr.left)
if curr.right:
q.append(curr.right)
result.append(temp)
return result