diff --git a/Maze.py b/Maze.py new file mode 100644 index 0000000..440f61b --- /dev/null +++ b/Maze.py @@ -0,0 +1,41 @@ +#Time complexity: O(k*m*n) where k times is the while loop that runs to determine when to stop the ball. m*n is the amount of times row,col added inside the queue. +#Space complexity: O(m*n) for queue data structure +#The intuition is to move the ball and once it stops do a BFS from that location by adding it to the while. If at any instance the row,col is equal to the destination return True otherwise False + +class Solution: + """ + @param maze: the maze + @param start: the start + @param destination: the destination + @return: whether the ball could stop at the destination + """ + def has_path(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool: + # write your code here + if start[0] == destination[0] and start[1] == destination[1]: + return True + + m = len(maze) + + n = len(maze[0]) + + directions = [[0,1],[1,0],[-1,0],[0,-1]] + + queue = deque() + queue.append(start) + maze[start[0]][start[1]] = 2 + + while queue: + row, col = queue.popLeft() + for dr, dc in directions: + while (row >= 0 and r < m and col>=0 and c < n and maze[row][col] != 1): + row += dr + col += dc + row -= dr + col -= dc + if row == destination[0] and col == destination[1]: return True + + if maze[row][col] != 2: + maze[row][col] = 2 + queue.append([row,col]) + + return False diff --git a/TownJudge.py b/TownJudge.py new file mode 100644 index 0000000..69a57de --- /dev/null +++ b/TownJudge.py @@ -0,0 +1,16 @@ +# Time complexity: O(n) +# Space: O(n) +# The intuition is to determine the indegress vs the outdegress for each and every element in the trust array. After that find if any individual/element value is n-1 meaning it is trusted by everybody but it doesn't trust nobody +# Then return the index otherwise return -1 + +class Solution: + def findJudge(self, n: int, trust: List[List[int]]) -> int: + arr = [0] * (n+1) + for ai, bi in trust: + arr[ai] -= 1 + arr[bi] += 1 + + for i in range(1, len(arr)): + if arr[i] == n - 1: + return i + return -1