From 063beb0ce58cdb868ac2ffd77590446950a26870 Mon Sep 17 00:00:00 2001 From: candida17 <53035166+candida17@users.noreply.github.com> Date: Fri, 17 Oct 2025 15:03:57 -0700 Subject: [PATCH 1/2] hasPath --- Problem2.java | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Problem2.java diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 0000000..c7b79bb --- /dev/null +++ b/Problem2.java @@ -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 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); + } +} From bf73795ab801e1b011852d2a8b7a1eda2bdb02a0 Mon Sep 17 00:00:00 2001 From: candida17 <53035166+candida17@users.noreply.github.com> Date: Fri, 17 Oct 2025 15:28:53 -0700 Subject: [PATCH 2/2] findJudge --- Problem1.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Problem1.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 0000000..91eef70 --- /dev/null +++ b/Problem1.java @@ -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; + } +}