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
25 changes: 25 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Time Complexity : O(n + t), t = number of trust pairs
# Space Complexity : O(n), to track trust counts
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Explanation:
# 1. Track two counts for each person: number of people they trust and number of people who trust them.
# 2. The town judge trusts nobody (trusts count = 0) and is trusted by n-1 people.
# 3. Scan all people and return the one satisfying these conditions. Return -1 if none.

from typing import List

class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
trusts = [0] * (n + 1) # number of people each person trusts
trusted_by = [0] * (n + 1) # number of people who trust each person

for a, b in trust:
trusts[a] += 1
trusted_by[b] += 1

for i in range(1, n + 1):
if trusts[i] == 0 and trusted_by[i] == n - 1:
return i
return -1
35 changes: 35 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Time Complexity : O(m * n), worst case visiting all cells
# Space Complexity : O(m * n), visited matrix
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Explanation:
# 1. Use DFS to explore the maze. From a cell, roll in each direction until hitting a wall.
# 2. Only mark the stopping cell as visited and continue DFS from there.
# 3. If we reach the destination, return True. Otherwise, continue exploring other directions.
# 4. Return False if all paths have been explored.

from typing import List

class Solution:
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
m, n = len(maze), len(maze[0])
visited = [[False] * n for _ in range(m)]

def dfs(x, y):
if [x, y] == destination:
return True
if visited[x][y]:
return False
visited[x][y] = True
for dx, dy in [(0,1),(1,0),(0,-1),(-1,0)]:
nx, ny = x, y
# roll until hitting wall
while 0 <= nx + dx < m and 0 <= ny + dy < n and maze[nx + dx][ny + dy] == 0:
nx += dx
ny += dy
if dfs(nx, ny):
return True
return False

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