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;
}
}
- Number of Islands: https://lnkd.in/drT2MpTz
- Rotting Oranges: https://lnkd.in/dUQVwJ-d
Arrays:
- Insert Interval: https://lnkd.in/dfcEDFwB
- 3Sum: https://lnkd.in/duGvuCjf
- Product of Array Except Self: https://lnkd.in/dkGkjQVk
- Combination Sum: https://lnkd.in/d3iStbGc
- Merge Intervals: https://lnkd.in/dmFZxrVQ
Stacks:
- Evaluate Reverse Polish Notation: https://lnkd.in/d-y7Zw4C
- Min Stack: https://lnkd.in/dqbh7PeV
- Trapping Rain Water: https://lnkd.in/dS_svBAm
Binary Trees:
- Binary Tree Level Order Traversal: https://lnkd.in/dM-VYbVB
- Lowest Common Ancestor of a Binary Tree: https://lnkd.in/dUvJykgA
- Serialize and Deserialize Binary Tree: https://lnkd.in/dW2cP5Wn
Dynamic Programming:
- Maximum Subarray: https://lnkd.in/dvjYye6E
- Coin Change: https://lnkd.in/d7zZRg7H
Binary Search:
- Search in Rotated Sorted Array: https://lnkd.in/dEuh3gie
- Time-Based Key-Value Store: https://lnkd.in/dbERGKUB
Strings:
- Longest Substring Without Repeating Characters: https://lnkd.in/d_vZrZda
- Minimum Window Substring: https://lnkd.in/de8aeeQD
Heap:
- K Closest Points to Origin: https://lnkd.in/dUtCqYf4
- Find Median from Data Stream: https://lnkd.in/ddDgWqUv
Recursion:
- Permutations: https://lnkd.in/dTUqmAfy