diff --git a/problem1.java b/problem1.java new file mode 100644 index 0000000..58f3203 --- /dev/null +++ b/problem1.java @@ -0,0 +1,29 @@ +// Time Complexity : O(n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +/* +Approach +we are using topological sort here , using array's index to represent people, if someone trusts a people we increase their count and decrease +the people who is trusting +in the next loop we check if someone has value equal to n-1 (n-1 mean part to the person it self everyone trusts him, and he does does not trust anyone or else n-1 is no possible) +we return that person +if that is not found that mean we don't have a judge +*/ + +class Solution { + public int findJudge(int n, int[][] trust) { + int[] indgrees = new int[n + 1]; // 0 + for (int[] tr : trust) { + indgrees[tr[0]]--; + indgrees[tr[1]]++; + } + for (int i = 1; i < indgrees.length; i++) { + if (indgrees[i] == n - 1) { + return i; + } + } + return -1; + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 0000000..d66ea45 --- /dev/null +++ b/problem2.java @@ -0,0 +1,49 @@ +// Time Complexity : O(nm) +// Space Complexity :O(nm) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +/* +Approach +we are suing dfs to find the destination, +add the start index to queue and make it a visted +then start the loop on queue until empty +in the loop we take one ele out from queue +and check in all dir until a hurdle is found or +when we take a set back check if its the desstination or not if yes return true +else if its not marked visited add it to queue, if queue is empty and destination not found return false +*/ +class Solution { + public boolean hasPath(int[][] maze, int[] start, int[] destination) { + if (maze == null || maze.length == 0) + return false; + if (start[0] == destination[0] && start[1] == destination[1]) + return true; + int m = maze.length; + int n = maze[0].length; + int[][] dirs = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } }; + Queue q = new LinkedList<>(); + q.add(start); + maze[start[0]][start[1]] = 2; + while (!q.isEmpty()) { + int[] cur = q.poll(); + for (int[] dir : dirs) { + int r = cur[0]; + int c = cur[1]; + while (r >= 0 && c >= 0 && r < m && c < n && maze[r][c] != 1) { + r += dir[0]; + c += dir[1]; + } + r -= dir[0]; + c -= dir[1]; + if (r == destination[0] && c == destination[1]) + return true; + if (maze[r][c] != 2) { + q.add(new int[] { r, c }); + maze[r][c] = 2; + } + } + } + return false; + } +} \ No newline at end of file