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
86 changes: 86 additions & 0 deletions TheMaze.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Time Complexity : O(m*n)
// Space Complexity : O(m*n))
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
// We traverse all 4 directions from a point and add the stop points to a queue
// Pop from the queue and mark as visited and repeat to find further stop points
// If we stop at the destination return true else false

import java.util.Queue;

class Solution {
int[][] dirs;
int m,n;

public boolean hasPathDFS(int[][] maze, int[] start, int[] destination) {
this.dirs = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
m = maze.length;
n = maze[0].length;
return dfs(maze, start, destination);
}

private boolean dfs(int[][] maze, int[] start, int[] destination){
if(start[0] == destination[0] && start[1] == destination[1]) return true;

maze[start[0]][start[1]] = 2;
for(int[] dir:dirs){
int r = start[0];
int c = start[1];

while(r>=0 && r<m && c>=0 && c<n && maze[r][c] != 1){
r += dir[0];
c += dir[1];
}

r -= dir[0];
c -= dir[1];

if(maze[r][c] != 2){
if(dfs(maze, new int[]{r,c}, destination)) return true;
}
}

return false;
}


public boolean hasPathBFS(int[][] maze, int[] start, int[] destination) {

this.dirs = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
m = maze.length;
n = maze[0].length;

Queue<int[]> q = new LinkedList<>();
q.offer(start);
maze[start[0]][start[1]] = 2;

while(!q.isEmpty()){
int[] curr = q.poll();
if(curr[0] == destination[0] && curr[1] == destination[1]) return true;

for(int[] dir:dirs){
int r = curr[0];
int c = curr[1];

while(r>=0 && r<m && c>=0 && c<n && maze[r][c] != 1){
r += dir[0];
c += dir[1];
}

r -= dir[0];
c -= dir[1];

if(r == destination[0] && c == destination[1]) return true;
if(maze[r][c] != 2){
q.offer(new int[]{r,c});
maze[r][c] = 2;
}
}
}

return false;
}
}
26 changes: 26 additions & 0 deletions TownJudge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity : O(V+E)
// Space Complexity : O(V)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
// Count the number of incoming and outgoing edges for each person
// for town judge, total of n-1 incoming edges and 0 outgoing edges
// indegree should be n-1

class Solution {
public int findJudge(int n, int[][] trust) {

int[] indegrees = new int[n+1];
for(int[] edge:trust){ //O(E)
indegrees[edge[0]]--;
indegrees[edge[1]]++;
}

for(int i=1;i<indegrees.length;i++) //O(V)
if(indegrees[i]==n-1) return i;

return -1;
}
}