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
26 changes: 26 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity : O(V + E), where V is number of people and E is number of trust pairs.
// Space Complexity :O(V), for storing indegrees and outDegrees arrays.
// 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 keep track of number of people trusting each other using indegree and outdegree
//when a trusts b outdeg will decrease and b indeg will increase
//in the end we need to check if the outdeg becomes 0 to satisfy the first condition - The town judge trusts nobody.
//and check for second condition Everybody (except for the town judge) trusts the town judge. - check if indeg becomes equal to 1 less number of people
class Solution {
public int findJudge(int n, int[][] trust) {
int[] outdeg = new int[n+1];
int[] indeg = new int[n+1];
for (int[] tr: trust) {
outdeg[tr[0]]--;
indeg[tr[1]]++;
}

for(int i = 1; i <= n; i++) {
if(outdeg[i] == 0 && indeg[i] == n-1) return i;
}
return -1;
}
}
68 changes: 68 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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
//Using BFS traversal we start by adding the start cell in the queue
//we then traverse up, down right and left of the maze until we find a wall and add them into the queue
//To prevent the visit of same cell again we mark the cells as visited whenever its being added to the queue
//If the destination is present inside the queue we return true

import java.util.*;
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int m = maze.length;
int n = maze[0].length;
int [][] dirs = new int[][] {{-1,0}, {1,0}, {0,-1}, {0,1}};
if(start[0] == destination[0] && start[1] == destination[1]) return true;
Queue<int []> q = new LinkedList<>();
q.add(start);//add the start indices into the queue (0.4)
maze[start[0]][start[1]] = 2; //mark the start indices as visited ie 0,4 is visited

while(!q.isEmpty()) {
int[] cur = q.poll();
for(int[] dir: dirs) {
int r = cur[0];
int c = cur[1];
//the next 0 to go in the queue is not the immediate neighbour but the one above the obstacle or wall
while(r < m && r >=0 && c>=0 && c< n && maze[r][c] != 1) {
r += dir[0];
c += dir[1];
}
r -= dir[0];
c -= dir[1];
//check if r and c are the destination
if(r== destination[0] && c == destination[1]) return true;
//only if the neighbours are not marked visited add them to queue
if(maze[r][c] !=2) {
maze[r][c]= 2;
q.add(new int[]{r,c});
}
}

}
return false;

}
}
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();

int[][] maze = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0}
};

int[] start = {0, 4};
int[] destination = {4, 4};

boolean result = sol.hasPath(maze, start, destination);
System.out.println(result);
}
}