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
49 changes: 49 additions & 0 deletions Maze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution(object):
# tc : O(n*m) worst case we end up moving to each cell,
# sc : O(n*m) recurssive stack space
def hasPath(self, maze, start, destination):
"""
:type maze: List[List[int]]
:type start: List[int]
:type destination: List[int]
:rtype: bool
"""
# connected components will be usign dfs approach to reach from the start t the destination
# will keep moving in a direction until it hit the obstacal or wall, then we check is this poistion the destination if not we check the 4 directions
# in the 4 directions we should avoid moving the previous block if not we will end up in a infinite loop
# so will keep a visited mark on the blocks so thatg we dont go into the same node again

self.m = len(maze)
self.n = len(maze[0])
self.dirs = [[-1,0], [1,0], [0,-1], [0,1]]

return self.dfs(maze,start[0],start[1],destination)

def dfs(self,maze,i,j,destination):
# base condtion
if i == destination[0] and j == destination[1]:
return True

# if we alredy visited this node then we just keep moving in circles and nevere reaching the destination
if maze[i][j] == -1: #-1 means the node is already visited
return False

# if none of the above conditions lets traverse, but first lets mark it visited
maze[i][j] = -1

for dir in self.dirs:
r = dir[0] + i
c = dir[1] + j

while r >=0 and r < self.m and c >= 0 and c < self.n and maze[r][c] != 1: # until we hit the obstcale we should keep moving in that direction
r += dir[0]
c += dir[1]

# until the last obstacle we are updating our r and col so the current position should be one behind
r -= dir[0]
c -= dir[1]

if self.dfs(maze,r,c,destination):
return True

return False
29 changes: 29 additions & 0 deletions TownJudge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution(object):
# tc. : O(V+E) other words O(n+m) where m is the len of the trust
# sc : O(n)
def findJudge(self, n, trust):
"""
:type n: int
:type trust: List[List[int]]
:rtype: int
"""
# two conditions to check
# town judge does nto depend on any other node and all the other nodes should depend on the town judge
# checking the indegrees of each node and if this node depends on other it strust value letsreduce it by 1 if other people trust this node lets increade the coutn by 1
# in the end the one with n-1 nodes trust is our judge

trust_count = [0]*(n+1) # each node intially 0 members as its 1-indexed people n+1

for relation in trust:
trust_count[relation[0]] -= 1# as this ndoe is trustign other person reduce its count, our judge does not trust anyone
trust_count[relation[1]] += 1

for i in range(1,n+1):
if trust_count[i] == n-1:
return i

return -1