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
24 changes: 24 additions & 0 deletions FindJudge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Time Complexity : O(V + E), where V is the number of people and E is the number of trust relationships.
# Space Complexity : O(V), for the indegrees array.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : We use an array to count trust levels: losing trust (out-degree) subtracts, gaining trust (in-degree) adds.
# The town judge should be trusted by everyone else but trust no one.
# So we just find the person with an in-degree of n−1.

class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
indegrees = [0] * (n+1)

for tr in trust:
indegrees[tr[0]] -= 1
indegrees[tr[1]] += 1

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

return -1



38 changes: 38 additions & 0 deletions TheMaze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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
# Approach : We simulate a ball rolling in the maze until it hits a wall in all 4 directions using BFS.
# Once it stops, we check if that position is the destination; if not visited, we enqueue it.
# We mark visited positions in-place by setting them to -1 to avoid cycles.

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

q = collections.deque()
q.append([start[0], start[1]])
maze[start[0]][start[1]] = -1

while q:
curr = q.popleft()
for dir in self.dirs:
r = dir[0] + curr[0]
c = dir[1] + curr[1]

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

r -= dir[0]
c -= dir[1]

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

return False