Skip to main content

25 Questions

Graphs:

Clone Graph

Course Schedule: https://lnkd.in/de8Q3NBS

01 Matrix

Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.

The distance between two cells sharing a common edge is 1.

Example 1:

Input: mat = [[0,0,0],[0,1,0],[0,0,0]] Output: [[0,0,0],[0,1,0],[0,0,0]]

class Solution {
    public int[][] updateMatrix(int[][] mat) {
        int m = mat.length;           // Number of rows
        int n = mat[0].length;        // Number of columns
        Queue<int[]> queue = new LinkedList<>(); // Queue to perform BFS

        // Initialize matrix:
        // Add all 0s to the queue and mark all 1s as unvisited (-1)
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(mat[i][j] == 0){
                    // Add coordinates of 0s to the queue as starting points
                    queue.add(new int[]{i, j});
                } else {
                    // Mark 1s as -1 to indicate they haven't been processed yet
                    mat[i][j] = -1;
                }
            }
        }

        // Directions to move in the matrix: up, down, left, right
        int[][] directions = new int[][]{
            {-1, 0}, // Up
            {1, 0},  // Down
            {0, -1}, // Left
            {0, 1}   // Right
        };

        // Perform BFS from all 0s simultaneously
        while(!queue.isEmpty()){
            int[] curr = queue.poll();          // Current cell coordinates
            int currRow = curr[0];
            int currCol = curr[1];

            // Explore all four directions
            for(int[] direction : directions){
                int row = currRow + direction[0];
                int col = currCol + direction[1];

                // Check bounds and whether the cell is unvisited
                if(row < 0 || row >= m || col < 0 || col >= n || mat[row][col] != -1){
                    continue; // Skip invalid or already visited cells
                }

                // Update the distance and enqueue the cell
                mat[row][col] = mat[currRow][currCol] + 1;
                queue.add(new int[]{row, col});
            }
        }

        // Return the updated matrix with minimum distances to 0
        return mat;
    }
}

  1. Number of Islands: https://lnkd.in/drT2MpTz
  2. Rotting Oranges: https://lnkd.in/dUQVwJ-d

Arrays:

  1. Insert Interval: https://lnkd.in/dfcEDFwB
  2. 3Sum: https://lnkd.in/duGvuCjf
  3. Product of Array Except Self: https://lnkd.in/dkGkjQVk
  4. Combination Sum: https://lnkd.in/d3iStbGc
  5. Merge Intervals: https://lnkd.in/dmFZxrVQ

Stacks:

  1. Evaluate Reverse Polish Notation: https://lnkd.in/d-y7Zw4C
  2. Min Stack: https://lnkd.in/dqbh7PeV
  3. Trapping Rain Water: https://lnkd.in/dS_svBAm

Binary Trees:

  1. Binary Tree Level Order Traversal: https://lnkd.in/dM-VYbVB
  2. Lowest Common Ancestor of a Binary Tree: https://lnkd.in/dUvJykgA
  3. Serialize and Deserialize Binary Tree: https://lnkd.in/dW2cP5Wn

Dynamic Programming:

  1. Maximum Subarray: https://lnkd.in/dvjYye6E
  2. Coin Change: https://lnkd.in/d7zZRg7H

Binary Search:

  1. Search in Rotated Sorted Array: https://lnkd.in/dEuh3gie
  2. Time-Based Key-Value Store: https://lnkd.in/dbERGKUB

Strings:

  1. Longest Substring Without Repeating Characters: https://lnkd.in/d_vZrZda
  2. Minimum Window Substring: https://lnkd.in/de8aeeQD

Heap:

  1. K Closest Points to Origin: https://lnkd.in/dUtCqYf4
  2. Find Median from Data Stream: https://lnkd.in/ddDgWqUv

Recursion:

  1. Permutations: https://lnkd.in/dTUqmAfy