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
36 changes: 36 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Time Complexity : O(V + E) where V is the total number of people and e is the edges present in the trust matrix
// 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

/*
I maintain an array inwardEdges wherein I for each person that a particular person trusts, I decrement it's corresponding value
by 1 in the array and increment the value for the person who is trusted by 1. After traversing all the links in the trust
array, I check to see that which person is trusted by all the other people and doesn't trust anyone themselves i.e value for inwardEdges
is n-1. That is my final answer. If such a person doesn't exist, I return -1.
*/

public class Solution {
public int FindJudge(int n, int[][] trust) {
int[] inwardEdges = new int[n];

foreach(int[] arr in trust)
{
inwardEdges[arr[0] - 1] -= 1;
inwardEdges[arr[1] - 1] += 1;
}

for(int i = 0; i < n; i++)
{
if(inwardEdges[i] == n-1)
{
return i+1;
}
}

return -1;
}
}
66 changes: 66 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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

/*
I perform a modified variant of BFS starting from the start coordinates. I mark it as visited and traverse in each direction until
I hit a wall or exceed the boundaries of the grid. When such a condition happens, I undo my last step and repeat the process from
that coordinate. If by doing so I reach the destination, I return true. Else after my queue becomes empty, I return false.
*/

public class Solution {
public bool HasPath(int[][] maze, int[] start, int[] destination) {
int[][] dirs = new int[][]
{
new int[] {0,1},
new int[] {0,-1},
new int[] {1,0},
new int[] {-1,0}
};

int rows = maze.Length;
int columns = maze[0].Length;
Queue<int[]> q = new();
q.Enqueue(new int[] {start[0], start[1]});

// Mark the coordinates as visited
maze[start[0]][start[1]] = 2;

while(q.Count != 0)
{
int[] temp = q.Dequeue();

foreach(int[] dir in dirs)
{

int r = temp[0], c = temp[1];

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

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

if(maze[r][c] != 2)
{
q.Enqueue(new int[] {r,c});
maze[r][c] = 2;

if(r == destination[0] && c == destination[1])
{
return true;
}
}
}
}

return false;
}
}