From 551eb4885e202bc4f51d430d53d348208241935d Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Tue, 7 Apr 2026 16:23:05 -0700 Subject: [PATCH 1/3] Problem1 and Problem2 added --- Problem1.py | 27 ++++++++++++++ Problem2.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 0000000..fc1dbba --- /dev/null +++ b/Problem1.py @@ -0,0 +1,27 @@ +# https://leetcode.com/problems/find-the-town-judge/description/ + +# Graph problem +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# TC: O(E) + O(V) +# SC: O(V) + +class Solution: + def findJudge(self, n: int, trust: List[List[int]]) -> int: + indegrees = [0] * (n+1) + + if n == 1: + return 1 + + for i in range(len(trust)): + tr = trust[i] + # giving trust + indegrees[tr[0]] -= 1 + # receiving trust + indegrees[tr[1]] += 1 + + for i in range(len(indegrees)): + if indegrees[i] == n-1: + return i + + return -1 \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 0000000..037534c --- /dev/null +++ b/Problem2.py @@ -0,0 +1,103 @@ +# https://leetcode.com/problems/the-maze/description/ + +# BFS with queue implementation as this problem needs to traverse the graph with connected components. +# TC: No walls => O(m*n) * O(m+n) +# SC: O(m*n) +from typing import List +from collections import deque + +class Solution: + def hasPath(self, maze: List[List[int]], start, destination) -> bool: + dirs = [(-1,0), (1,0), (0,-1), (0,1)] + m = len(maze) + n = len(maze[0]) + + q = deque() + q.append(start) + + # mark visited + maze[start[0]][start[1]] = -1 + + while q: + curr = q.popleft() + + for dir in dirs: + r = dir[0] + curr[0] + c = dir[1] + curr[1] + + while r >= 0 and c >= 0 and r < m and c < 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 + +if __name__ == "__main__": + maze = [[0,0,1,0,0], + [0,0,0,0,0], + [0,0,0,1,0], + [1,1,0,1,1], + [0,0,0,0,0]] + + start = [0, 4] + destination = [4, 4] + + sol = Solution() + print(sol.hasPath(maze, start, destination)) + +# DFS Solution +# TC: O(m*n) +# SC: O(m*n) +from typing import List + +class Solution: + def hasPath(self, maze: List[List[int]], start, destination) -> bool: + self.dirs = [(-1,0), (1,0), (0,-1), (0,1)] + self.m = len(maze) + self.n = len(maze[0]) + + return self.helper(maze, start[0], start[1], destination) + + def helper(self, maze, i, j, destination) -> bool: + + if i == destination[0] and j == destination[1]: return True + if maze[i][j] == -1: return False + + maze[i][j] = -1 + + for dir in self.dirs: + r = dir[0] + i + c = dir[1] + j + + 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 self.helper(maze, r, c, destination): return True + + return False + + +if __name__ == "__main__": + maze = [[0,0,1,0,0], + [0,0,0,0,0], + [0,0,0,1,0], + [1,1,0,1,1], + [0,0,0,0,0]] + + start = [0, 4] + destination = [4, 4] + + sol = Solution() + print(sol.hasPath(maze, start, destination)) \ No newline at end of file From 620ea82286fb8e710c5c35732468ef7ec0b0814f Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Tue, 7 Apr 2026 16:26:50 -0700 Subject: [PATCH 2/3] Comments added --- Problem1.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Problem1.py b/Problem1.py index fc1dbba..e1a1cb3 100644 --- a/Problem1.py +++ b/Problem1.py @@ -3,8 +3,9 @@ # Graph problem # Did this code successfully run on Leetcode : Yes # Any problem you faced while coding this : No -# TC: O(E) + O(V) -# SC: O(V) +# TC: O(E) + O(V) => We need to first go over the entire input array of tuples (edges) to construct the indegrees +# then we need to traverse the entire indegrees array of size V +# SC: O(V) => indegrees array of size V class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: From 9cff9033aba247c64ea751fafcb4e473034fad52 Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Tue, 7 Apr 2026 19:30:13 -0700 Subject: [PATCH 3/3] Correction added --- Problem2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Problem2.py b/Problem2.py index 037534c..34bf6ee 100644 --- a/Problem2.py +++ b/Problem2.py @@ -77,7 +77,7 @@ def helper(self, maze, i, j, destination) -> bool: r = dir[0] + i c = dir[1] + j - while r >=0 and c >= 0 and r < self.m and c < self.n and maze[r][c] != -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]