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
86 changes: 86 additions & 0 deletions Leetcode490.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Solution - DFS Approach
// TC - O(m*n) - iterate over maze once.
// SC - O(m*n) - recursive stack space. Worst case we have to visit all cells in the maze.
// class Solution {
// public boolean hasPath(int[][] maze, int[] start, int[] destination) {
// int[][] dirs = new int[][] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
// maze[start[0]][start[1]] = 2;
// return helper(maze, start, destination, dirs);
// }

// private boolean helper(int[][] maze, int[] start, int[] destination, int[][] dirs) {
// // base
// if (start[0] == destination[0] && start[1] == destination[1]) {
// return true;
// }
// // Logic
// // Recurse
// for (int[] dir : dirs) { // for each direction, keep moving in that direction until we hit a wall or
// // boundary.
// int r = start[0];
// int c = start[1];
// while (r >= 0 && r < maze.length && c < maze[0].length && c >= 0 && maze[r][c] != 1) { // keep moving in
// // that direction
// // until we hit a
// // wall or boundary.
// r += dir[0];
// c += dir[1];
// }
// r -= dir[0]; // move back to the last valid position.
// c -= dir[1]; // move back to the last valid position.
// if (maze[r][c] != 2) { // if we have not visited this cell before, then only recurse. This is important
// // to avoid infinite loop.
// maze[r][c] = 2;
// if (helper(maze, new int[] { r, c }, destination, dirs)) { // recurse with new position as start
// // position.
// return true; // if we found a path to destination, return true.
// }
// }
// }
// return false;

// }
// }

// Solution 2: BFS Approach
// TC - O(m*n) - iterate over maze once.
// SC - O(m*n) - queue space. Worst case we have to visit all cells in the maze.
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
Queue<int[]> q = new LinkedList<>();
q.add(start);
int[][] dirs = new int[][] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
maze[start[0]][start[1]] = 2;

while (!q.isEmpty()) { // iterate over queue until it is empty.
int[] current = q.poll();
int r = current[0];
int c = current[1];
if (r == destination[0] && c == destination[1])
return true; // if we found a path to destination, return true.

for (int[] dir : dirs) { // for each direction, keep moving in that direction until we hit a wall or
// boundary.
r = current[0];
c = current[1];
while (r >= 0 && r < maze.length && c >= 0 && c < maze[0].length && maze[r][c] != 1) { // keep moving in
// that direction
// until we hit a
// wall or
// boundary.
r += dir[0];
c += dir[1];
}
r -= dir[0]; // move back to the last valid position.
c -= dir[1]; // move back to the last valid position.
if (maze[r][c] != 2) { // if we have not visited this cell before, then only add it to the queue. This
// is important to avoid infinite loop.
maze[r][c] = 2;
q.add(new int[] { r, c }); // add new position to the queue.
}
}

}
return false;
}
}
22 changes: 22 additions & 0 deletions Leetcode997.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Solution - HashMap Approach
// TC - O(n) - iterate over trust array once.
// SC - O(n) - HashMap space. Worst case all people trust the judge.
class Solution {
public int findJudge(int n, int[][] trust) {
if (n == 1)
return 1;
HashMap<Integer, Integer> map = new HashMap<>();
for (int[] t : trust) {
map.put(t[1], map.getOrDefault(t[1], 0) + 1); // increment trust count for the person who is being trusted.
map.put(t[0], map.getOrDefault(t[0], 0) - 1); // decrement trust count for the person who is trusting
// someone else.
}
for (int i = 1; i <= n; i++) { // iterate over all people and check if there is a person who is trusted by n-1
// people and does not trust anyone else.
if (map.containsKey(i) && map.get(i) == n - 1)
return i;
}
return -1;

}
}