diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 0000000..0158ab5 --- /dev/null +++ b/Problem1.cs @@ -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; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 0000000..a09094e --- /dev/null +++ b/Problem2.cs @@ -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 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=0 && c