From 0c4847c68902b8bf6525554b4d1a78dfbcb42c83 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 10 Jan 2026 13:06:08 -0500 Subject: [PATCH 1/2] indegrees and outdegrees --- find_the_town_judge.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 find_the_town_judge.py diff --git a/find_the_town_judge.py b/find_the_town_judge.py new file mode 100644 index 0000000..1f0b05c --- /dev/null +++ b/find_the_town_judge.py @@ -0,0 +1,30 @@ +# https://leetcode.com/problems/find-the-town-judge/ +# Time Complexity-O(v+e) Space Complexity- O(v) + +class Solution: + def findJudge(self, n, trust): + # create two arrays one for incoming and other for outgoing + indegrees = [0] * (n + 1) + outDegrees = [0] * (n + 1) + + # iterate through the trust array + for tr in trust: + # increase the number if outgoing + outDegrees[tr[0]] += 1 + # increase the number if incoming + indegrees[tr[1]] += 1 + + # iterate through the range + for i in range(1, n + 1): + # check if the number is trusted by everyone except the current number + if indegrees[i] == n - 1 and outDegrees[i] == 0: + return i + # return -1 if no town judge exist + return -1 + + +solution = Solution() +print(solution.findJudge(2,[[1,2]])) + + + \ No newline at end of file From e04900b6a7a9e2e39bcf44d3ae45ac483cc6741a Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 10 Jan 2026 15:02:46 -0500 Subject: [PATCH 2/2] BFS approach --- the_maze.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 the_maze.py diff --git a/the_maze.py b/the_maze.py new file mode 100644 index 0000000..ee0aa4b --- /dev/null +++ b/the_maze.py @@ -0,0 +1,50 @@ +# https://leetcode.com/problems/the-maze/ +# Time Complexity- O(m*n) Space Complexity- O(m*n) + +from collections import deque +class Solution: + def hasPath(self, maze, start, destination): + # initialising the direction array + self.dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]] + # finding the rows of the maze + self.m = len(maze) + # finding the columns of the maze + self.n = len(maze[0]) + + # intialise the queue + q = deque() + # append the queue with the starting position + q.append([start[0], start[1]]) + # mark the visited position as -1 to prevent it visiting again + maze[start[0]][start[1]] = -1 + + while q: + # pop the element from the queue which is the current cell([r,c]) + curr = q.popleft() + # check all 4 directions of the ball + for dir in self.dirs: + # start moving one step in the current direction from the current cell + r = dir[0] + curr[0] + c = dir[1] + curr[1] + # check the condition of out of bounce and obstacles + while r >= 0 and c >= 0 and r < self.m and c < self.n and maze[r][c] != 1: + #keep moving thte ball in that direction until it hits the wall + r += dir[0] + c += dir[1] + # step back by one cell because ball hits the wall + r -= dir[0] + c -= dir[1] + # check if the stopping point is the given destination + if r == destination[0] and c == destination[1]: + return True + # check the stopping point is not visited before + if maze[r][c] != -1: + # append the queue with new position + q.append([r,c]) + # mark it as visited + maze[r][c] = -1 + + return False + +solution = Solution() +print(solution.hasPath([[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]],[0,4],[3,2])) \ No newline at end of file