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
18 changes: 18 additions & 0 deletions find-the-town-judge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
''' Time Complexity : O(V + E)
Space Complexity : O(V)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

'''

class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
indegree = [0] * (n+1)
for i,j in trust:
indegree[j] += 1
indegree[i] -= 1
for i in range(1,n+1):
if indegree[i] == n-1:
return i

return -1
77 changes: 77 additions & 0 deletions the-maze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#--------Solution 1 : BFS---------------
''' Time Complexity : O(m*n)
Space Complexity : O(m*n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

'''

class Solution:
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
m, n = len(maze),len(maze[0])
dirs = [(0,1),(0,-1),(1,0),(-1,0)]
q = deque()
q.append((start[0],start[1]))
maze[start[0]][start[1]] = 2
while q:
i, j = q.pop()
for dir in dirs:
r = dir[0] + i
c = dir[1] + j

while 0<=r<m and 0<=c<n and maze[r][c] != 1:
r = dir[0] + r
c = dir[1] + c

#step back
r = -dir[0] + r
c = -dir[1] + c

if destination[0] == r and destination[1] == c:
return True
if maze[r][c] != 2:
maze[r][c] = 2
q.append((r,c))

return False

#--------Solution 2 : DFS---------------
''' Time Complexity : O(m*n)
Space Complexity : O(m*n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

'''
class Solution:
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
m, n = len(maze),len(maze[0])
dirs = [(0,1),(0,-1),(1,0),(-1,0)]
self.destination = destination

def dfs(i, j, maze):
if [i,j] == destination:
return True
maze[i][j] = 2

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

while 0<=r<m and 0<=c<n and maze[r][c] != 1:
r = dir[0] + r
c = dir[1] + c

#step back
r = -dir[0] + r
c = -dir[1] + c

if maze[r][c] != 2:
if dfs(r,c,maze):
return True
return False

return dfs(start[0],start[1],maze)