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
34 changes: 34 additions & 0 deletions Maze-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This solution uses a BFS approach to grab the neighbors first
// On each direction of neighbor we roll the ball until valid and add it to the queue
// We change the number to -1 if a column is visited
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int m = maze.length;
int n = maze[0].length;
Queue<int[]> queue = new LinkedList();

queue.add(start);
maze[start[0]][start[1]]=-1;
int[][] dirs = {{0,1}, {0,-1}, {1,0}, {-1,0}};
while(!queue.isEmpty()) {
int[] dest = queue.poll();

for(int i=0;i<dirs.length;i++) {
row = dest[0];
col = dest[1];
int[] dir = dirs[i];
while((row+dir[0]>=0 && col+dir[1]>=0 && row+dir[0]<=m-1 && col+dir[1]<=n-1) && maze[row+dir[0]][col+dir[1]]!=1) {
row = row+dir[0];
col = col+dir[1];
}
if(maze[row][col]!=-1) {
if(row==destination[0] && col==destination[1]) return true;
queue.add(new int[]{row, col});
maze[row][col]=-1;
}
}
}

return false;
}
}
20 changes: 20 additions & 0 deletions TownJudge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This solution uses an indegree array which will track inward and outward edges to an element.
// For all the inward edges, we increment and outward we decrement
// At the end, if there is any edge with n-1 val then its the judge
class Solution {
public int findJudge(int n, int[][] trust) {
int[] indegree = new int[n];
Arrays.fill(indegree, 0);
for(int i=0;i<trust.length;i++) {
int[] temp = trust[i];
indegree[temp[1]-1]++;
indegree[temp[0]-1]--;
}
for(int i=0;i<n;i++) {
if(indegree[i]==n-1) {
return i+1;
}
}
return -1;
}
}