Skip to content

Instantly share code, notes, and snippets.

@sharunkumar
Last active April 5, 2023 18:59
Show Gist options
  • Save sharunkumar/2d204f1e04f88d7d0f807721a0e8047a to your computer and use it in GitHub Desktop.
Save sharunkumar/2d204f1e04f88d7d0f807721a0e8047a to your computer and use it in GitHub Desktop.
DFS and BFS
// BFS
class Node {
int row;
int col;
int step;
Node(int row, int col, int step) {
this.row = row;
this.col = col;
this.step = step;
}
}
class Solution {
public int[][] updateMatrix(int[][] mat) {
int n = mat.length;
int m = mat[0].length;
int vis[][] = new int[n][m];
int dis[][] = new int[n][m];
Queue<Node> q = new LinkedList<Node>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == 0) {
vis[i][j] = 1;
q.add(new Node(i, j, 0));
} else {
vis[i][j] = 0;
}
}
}
int delRow[] = { -1, 1, 0, 0 };
int delCol[] = { 0, 0, 1, -1 };
while (!q.isEmpty()) {
int row = q.peek().row;
int col = q.peek().col;
int step = q.peek().step;
q.remove();
dis[row][col] = step;
for (int i = 0; i < 4; i++) {
int nrow = row + delRow[i];
int ncol = col + delCol[i];
if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && vis[nrow][ncol] == 0) {
vis[nrow][ncol] = 1;
q.add(new Node(nrow, ncol, step + 1));
}
}
}
return dis;
}
}
// BFS
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
long sum = 0, count = 0;
Queue<TreeNode> temp = new LinkedList<>();
while (!queue.isEmpty()) {
TreeNode n = queue.remove();
sum += n.val;
count++;
if (n.left != null)
temp.add(n.left);
if (n.right != null)
temp.add(n.right);
}
queue = temp;
res.add(sum * 1.0 / count);
}
return res;
}
}
// DFS
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color != newColor)
dfs(image, sr, sc, color, newColor);
return image;
}
public void dfs(int[][] image, int r, int c, int color, int newColor) {
if (image[r][c] == color) {
image[r][c] = newColor;
if (r >= 1)
dfs(image, r - 1, c, color, newColor);
if (c >= 1)
dfs(image, r, c - 1, color, newColor);
if (r + 1 < image.length)
dfs(image, r + 1, c, color, newColor);
if (c + 1 < image[0].length)
dfs(image, r, c + 1, color, newColor);
}
}
}
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color == newColor) {
return image;
}
int rows = image.length;
int cols = image[0].length;
Stack<int[]> stack = new Stack<>();
stack.push(new int[] { sr, sc });
while (!stack.isEmpty()) {
int[] curr = stack.pop();
int r = curr[0];
int c = curr[1];
if (image[r][c] == color) {
image[r][c] = newColor;
if (r >= 1) {
stack.push(new int[] { r - 1, c });
}
if (c >= 1) {
stack.push(new int[] { r, c - 1 });
}
if (r + 1 < rows) {
stack.push(new int[] { r + 1, c });
}
if (c + 1 < cols) {
stack.push(new int[] { r, c + 1 });
}
}
}
return image;
}
}
// BFS
class Solution {
// run the rotting process, by marking the rotten oranges with the timestamp
public boolean runRottingProcess(int timestamp, int[][] grid, int ROWS, int COLS) {
int[][] directions = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
// flag to indicate if the rotting process should be continued
boolean toBeContinued = false;
for (int row = 0; row < ROWS; ++row)
for (int col = 0; col < COLS; ++col)
if (grid[row][col] == timestamp)
// current contaminated cell
for (int[] d : directions) {
int nRow = row + d[0], nCol = col + d[1];
if (nRow >= 0 && nRow < ROWS && nCol >= 0 && nCol < COLS)
if (grid[nRow][nCol] == 1) {
// this fresh orange would be contaminated next
grid[nRow][nCol] = timestamp + 1;
toBeContinued = true;
}
}
return toBeContinued;
}
public int orangesRotting(int[][] grid) {
int ROWS = grid.length, COLS = grid[0].length;
int timestamp = 2;
while (runRottingProcess(timestamp, grid, ROWS, COLS))
timestamp++;
// end of process, to check if there are still fresh oranges left
for (int[] row : grid)
for (int cell : row)
// still got a fresh orange left
if (cell == 1)
return -1;
// return elapsed minutes if no fresh orange left
return timestamp - 2;
}
}
// BFS
class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
TreeNode t1 = q.poll();
TreeNode t2 = q.poll();
if (t1 == null && t2 == null)
continue;
if (t1 == null || t2 == null)
return false;
if (t1.val != t2.val)
return false;
q.add(t1.left);
q.add(t2.right);
q.add(t1.right);
q.add(t2.left);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment