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
44 changes: 44 additions & 0 deletions ballinmaze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Time Complexity : O(N*M) where N * M is the size of the matrix
# Space Complexity : O(N*M) as we are using a queue to store the positions
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Your code here along with comments explaining your approach:
# I am using BFS to solve this problem.
# I am starting from the start position and exploring all possible paths.
# I am using a queue to store the positions to be explored.
# I am marking the visited positions by changing their value to 2.
# I am doing this to save space instead of using a separate visited array.
# I am exploring all four directions from each position and moving until I hit a wall.
# If the final position is the destination, then return True.
# If no path is found, return False.

from typing import List
from collections import deque
class Solution:
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
m = len(maze)
n = len(maze[0])
dirs = [(0,1),(1,0),(-1,0),(0,-1)]
q = deque([(start[0], start[1])])
maze[start[0]][start[1]] = 2
while q:
r,c = q.popleft()
for dr,dc in dirs:
nr = r + dr
nc = c + dc
while nr < m and nr >= 0 and nc < n and nc >= 0 and maze[nr][nc] != 1:
nr += dr
nc += dc
nr -= dr
nc -= dc
if nr == destination[0] and nc == destination[1]:
return True
if maze[nr][nc] != 2:
q.append((nr,nc))
maze[nr][nc] = 2
return False




25 changes: 25 additions & 0 deletions townjudge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Time Complexity : O(N + t) where N is the number of people and t is the number of trust relationships
# Space Complexity : O(N) as we are using two arrays of size N+1
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Your code here along with comments explaining your approach:
# I am using two arrays to keep track of the in-degrees and out-degrees of each person.
# I am iterating through the trust list and for each trust relationship, I am incrementing
# the in-degree of the person being trusted and the out-degree of the person who trusts.
# Finally, I am checking for a person who has in-degree of n-1 and out-degree of 0.
# If such a person exists, I return their label, otherwise I return -1.

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