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
45 changes: 45 additions & 0 deletions TheMaze.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity : O(m*n)
// Space Complexity : O(m*n)
// Did this code successfully run on Leetcode : Yes
// Approach : We perfrom BFS and explore the path of not crossing boundaries and not hitting the wall. We maintain a queue and add the start position to the queue first. Then explore all
// four directions from that point and check if we crossed the boundaries or hit the wall. If we hit the wall, we step back, add to queue if not previously visited and mark it visited.
// When we stop, we check if it is destination index and return true else continue to explore until queue is empty, that means processing of all indexes is done.

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

public boolean hasPath(int[][] maze, int[] start, int[] destination) {
this.dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1}}; //all four directions array
this.m = maze.length;
this.n = maze[0].length;

Queue<int[]> q = new LinkedList<>();//using queue to perform BFS
q.add(new int[]{start[0], start[1]});//add the start point to the queue to start processing
maze[start[0]][start[1]] = 2; //mark it as visited by updating with 2

while(!q.isEmpty()){
int[] curr = q.poll(); //process current row-column index
for(int[] dir: dirs){ //explore all four directions
int r = dir[0] + curr[0];
int c = dir[1] + curr[1];

while(r>=0 && c>=0 && r<m && c<n && maze[r][c] != 1){ //bounds hit and not the wall check
r += dir[0];
c += dir[1];
}

r -= dir[0];//step back because we either crossed the bounds or hit the wall
c -= dir[1];

if(r == destination[0] && c == destination[1]) return true; //reached destination point
if(maze[r][c] != 2){ //if not visited
q.add(new int[]{r,c}); //add to queue
maze[r][c] = 2; //make it visited
}
}
}

return false;
}
}
21 changes: 21 additions & 0 deletions TownJudge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Time Complexity : O(V+E). where V is the number of people, E is the trusted relationships
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Approach : We maintain a indegrees array and increment the index if we find a trusted one and decrement if we find a trustee. This count if equals n-1 implies
// that he is trusted by everyone except self and he doesn't trust anybody.

class Solution {
public int findJudge(int n, int[][] trust) {
int[] indegrees = new int[n + 1];
for (int[] tr : trust) {
indegrees[tr[0]]--; //person who trusts
indegrees[tr[1]]++; //person who gets trusted
}
for (int i = 1; i <= n; i++) {
if (indegrees[i] == n - 1) { //trusted by all except self and does't trust anyone
return i;
}
}
return -1;
}
}