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
21 changes: 21 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//TC: O(m + n)
//SC: O(n)
// m is size of the trust array


class Solution {
public int findJudge(int n, int[][] trust) {
if(n == 0) return -1;
int[] indegree = new int[n];
for(int[] edge: trust){
indegree[edge[0]-1]--;
indegree[edge[1]-1]++;
}
for(int i = 0; i < n; i++){
if(indegree[i] == n -1){
return i+1;
}
}
return -1;
}
}
32 changes: 32 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// TC: O(m * n)
// SC: O(m * n)
class Solution {
int[][] dirs;
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if(maze.length == 0 || maze[0].length == 0) return false;
dirs = new int[][]{{1,0},{0,1},{-1, 0}, {0, -1}};
Queue<int[]> q = new LinkedList<>();
q.add(start);
maze[start[0]][start[1]]=2;
while(!q.isEmpty()){
int[] cordinate = q.poll();
for(int[] dir: dirs){
int nr = dir[0] + cordinate[0];
int nc = dir[1] + cordinate[1];
while(nr >= 0 && nc >= 0 && nr < maze.length && nc < maze[0].length && maze[nr][nc] != 1){
nr = nr + dir[0];
nc = nc + dir[1];
}
nr = nr - dir[0];
nc = nc - dir[1];
if(nr == destination[0] && nc == destination[1])
return true;
if(maze[nr][nc] != 2){
maze[nr][nc] = 2;
q.add(new int[]{nr,nc});
}
}
}
return false;
}
}