From de14e712ded098567ae008ee15a6015d21fa5769 Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Sat, 4 Apr 2026 17:25:31 -0700 Subject: [PATCH] Done Graph-1 --- FindJudge.java | 28 ++++++++++++++++++++++++++ TheMaze.java | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 FindJudge.java create mode 100644 TheMaze.java diff --git a/FindJudge.java b/FindJudge.java new file mode 100644 index 0000000..679a887 --- /dev/null +++ b/FindJudge.java @@ -0,0 +1,28 @@ +// Time Complexity : O(E), number of trust relationships (edges) +// Space Complexity : O(N) , number of people +// 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 maintain a scores array to keep track of relationship scores for each person. We iterate through the trust +relationships which is array of edges and decrement the score for person who is trusting i.e,x and increment +the score for person who is getting trusted, i.e., y. Lastly, we go through the scores array and check if any +person is trusted by all n-1 people, if so, we return that person's label. If not, -1. + */ +class Solution { + public int findJudge(int n, int[][] trust) { + int[] scores = new int[n + 1]; + + for(int[] relation : trust) { + scores[relation[0]]--; + scores[relation[1]]++; + } + + for(int i = 1 ; i <= n ; i++) { + if(scores[i] == n - 1) + return i; + } + return -1; + } +} \ No newline at end of file diff --git a/TheMaze.java b/TheMaze.java new file mode 100644 index 0000000..9c30b71 --- /dev/null +++ b/TheMaze.java @@ -0,0 +1,53 @@ +// Time Complexity : O(mn) +// Space Complexity : O(mn) +// 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 do a dfs recursive approach to roll the ball from the start position until destination coordinates are met. +As mentioned in the problem, the ball can be rolled in 4 directions, so we declare a dirs array and iterate the +i,j coordinates in all 4 directions. We need to roll ball until a wall is hit, so we use a while loop to iterate +with the given direction until conditions go out of bounds.Now, we decrement to the previous position and explore +in all directions from that position and we also keep track of visited array to mark positions such that we +dont visit them again. + */ +class Solution { + boolean flag; + int[][] dirs; + int m, n; + public boolean hasPath(int[][] maze, int[] start, int[] destination) { + this.flag = false; + this.dirs = new int[][] {{-1, 0} , {0, -1} , {1, 0} , {0, 1}}; + this.m = maze.length; + this.n = maze[0].length; + boolean[][] visited = new boolean[m][n]; + dfs(maze, start[0] , start[1], destination, visited); + return flag; + } + + private void dfs(int[][] maze, int i , int j, int[] destination, boolean[][] visited) { + if(i == destination[0] && j == destination[1]) { + flag = true; + return; + } + + visited[i][j] = true; + + for(int[] dir : dirs) { + int nr = dir[0] + i; + int nc = dir[1] + j; + + while(nr >= 0 && nc >= 0 && nr < m && nc < n && maze[nr][nc] == 0) { + nr += dir[0]; + nc += dir[1]; + } + + nr -= dir[0]; + nc -= dir[1]; + + if(!visited[nr][nc]) + dfs(maze, nr, nc , destination, visited); + } + } +} \ No newline at end of file