Bfs
BFS
Forty questions focused on breadth-first search, for shortest-path and level-order problems.
Find if Path Exists in Graph
easyThere is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).
Flood Fill
easyYou are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image.
Island Perimeter
easyYou are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
01 Matrix
mediumGiven an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
Accounts Merge
mediumGiven a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails...
All Paths From Source to Target
mediumGiven a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.
As Far from Land as Possible
mediumGiven an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized,...
Bulb Switcher II
mediumThere is a room with n bulbs labeled from 1 to n that all are turned on initially, and four buttons on the wall.
Cheapest Flights Within K Stops
mediumThere are n cities connected by some number of flights.
Check if There is a Valid Path in a Grid
mediumYou are given an m x n grid.
Check Knight Tour Configuration
mediumThere is a knight on an n x n chessboard.
Coin Change
mediumYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Coloring A Border
mediumYou are given an m x n integer matrix grid, and three integers row, col, and color.
Count Islands With Total Value Divisible by K
mediumYou are given an m x n matrix grid and a positive integer k.
Count Servers that Communicate
mediumYou are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server.
Count Sub Islands
mediumYou are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land).
Count the Number of Complete Components
mediumYou are given an integer n.
Count the Number of Houses at a Certain Distance I
mediumYou are given three positive integers n, x, and y.
Count Unreachable Pairs of Nodes in an Undirected Graph
mediumYou are given an integer n.
Course Schedule
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule II
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule IV
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Detect Cycles in 2D Grid
mediumGiven a 2D array of characters grid of size m x n, you need to find if there exists any cycle consisting of the same value in grid.
Detonate the Maximum Bombs
mediumYou are given a list of bombs.
Disconnect Path in a Binary Matrix by at Most One Flip
mediumYou are given a 0-indexed m x n binary matrix grid.
Evaluate Division
mediumYou are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i].
Find a Safe Walk Through a Grid
mediumYou are given an m x n binary matrix grid and an integer health.
Find All Groups of Farmland
mediumYou are given a 0-indexed m x n binary matrix land where a 0 represents a hectare of forested land and a 1 represents a hectare of farmland.
Find Eventual Safe States
mediumThere is a directed graph of n nodes with each node labeled from 0 to n - 1.
Find the Safest Path in a Grid
mediumYou are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: You are initially positioned at cell (0, 0).
Flower Planting With No Adjacent
mediumYou have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi.
Get Watched Videos by Your Friends
mediumThere are n people, each person has a unique id between 0 and n-1.
Grid Teleportation Traversal
mediumYou are given a 2D character grid matrix of size m x n, represented as an array of strings, where matrix[i][j] represents the cell at the intersection of the ith row and jth column.
Is Graph Bipartite?
mediumThere is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.
Jump Game III
mediumGiven an array of non-negative integers arr, you are initially positioned at start index of the array.
K Highest Ranked Items Within a Price Range
mediumYou are given a 0-indexed 2D integer array grid of size m x n that represents a map of the items in a shop.
Keys and Rooms
mediumThere are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0.
Lexicographically Smallest String After Applying Operations
mediumYou are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.
Map of Highest Peak
mediumYou are given an integer matrix isWater of size m x n that represents a map of land and water cells.
Max Area of Island
mediumYou are given an m x n binary matrix grid.