diff --git a/Maze.java b/Maze.java new file mode 100644 index 0000000..04bbef1 --- /dev/null +++ b/Maze.java @@ -0,0 +1,53 @@ +import java.util.LinkedList; +import java.util.Queue; + + +/* +Time Complexity : O(m*n) +Space Complexity : O(m*n) +Approach : +We can maintain the queue of all the cells where the ball is going to stop. The cells just before a wall. We +start the ball in all 4 dirs, if there is a cell where it is going to stop and is already not visited, add them to the queue. +If one such cell is the destination return true, else the ball is never going to stop at the destination. So + +1. Find neighbors where ball can stop by going in 1 direction. Add it to the queue. +2. Mark the cell visited so that it is not visited again. + +*/ + +public class Maze { + public boolean hasPath(int[][] maze, int[] start, int[] destination) { + int rows = maze.length; + int cols = maze[0].length; + + Queue queue = new LinkedList<>(); + queue.add(start); + maze[start[0]][start[1]] = 2; + int[][] dirs = {{0,1}, {1,0}, {-1,0},{0,-1}}; + while(!queue.isEmpty()){ + int[] curr = queue.poll(); + for(int[] dir : dirs){ + int nr = dir[0]+ curr[0]; + int nc = dir[1]+curr[1]; + // Move till you encounter a wall + while(nr >= 0 && nc >= 0 && nr < rows && nc < cols && maze[nr][nc] != 1){ + nr = nr + dir[0]; + nc = nr + dir[1]; + } + // move back one step + nr -= dir[0]; + nc -= dir[1]; + // if the place we stopped at, is the destination return true. + if(nr == destination[0] && nc == destination[1]) return true; + // If the cell is not visited already + if(maze[nr][nc] != 2) { + queue.add(new int[]{nr, nc}); + maze[nr][nc] = 2; + } + } + + } + return false; + } + +} diff --git a/TownJudge.java b/TownJudge.java new file mode 100644 index 0000000..2c0833a --- /dev/null +++ b/TownJudge.java @@ -0,0 +1,25 @@ +/* +Time Complexity : O(n) - n people in the town +Space Complexity : O(n) - extra array +Approach : +We can maintain an indegree array for each person in town. If a person A trusts person B, we increase B's indegree and +decrease A's indegree. Since all people should trust the judge, its indegree should be n-1. Since the judge trusts no one, +its indegree should remain at n-1. If no such person exists, we should return -1. +*/ +public class TownJudge { + public int findJudge(int n, int[][] trusts) { + int[] indegree = new int[n+1]; + + for(int[] trust :trusts){ + indegree[trust[0]]--; + indegree[trust[1]]++; + } + + for(int i = 1; i< n+1; i ++){ + if(indegree[i] == n-1){ + return i; + } + } + return -1; + } +}