Bfs

BFS

Forty questions focused on breadth-first search, for shortest-path and level-order problems.

Find if Path Exists in Graph

easy
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).

Flood Fill

easy
You 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

easy
You 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

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

Accounts Merge

medium
Given 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

medium
Given 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

medium
Given 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

medium
There 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

medium
There are n cities connected by some number of flights.

Check if There is a Valid Path in a Grid

medium
You are given an m x n grid.

Check Knight Tour Configuration

medium
There is a knight on an n x n chessboard.

Coin Change

medium
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Coloring A Border

medium
You are given an m x n integer matrix grid, and three integers row, col, and color.

Count Islands With Total Value Divisible by K

medium
You are given an m x n matrix grid and a positive integer k.

Count Servers that Communicate

medium
You 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

medium
You 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

medium
You are given an integer n.

Count the Number of Houses at a Certain Distance I

medium
You are given three positive integers n, x, and y.

Count Unreachable Pairs of Nodes in an Undirected Graph

medium
You are given an integer n.

Course Schedule

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule II

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule IV

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Detect Cycles in 2D Grid

medium
Given 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

medium
You are given a list of bombs.

Disconnect Path in a Binary Matrix by at Most One Flip

medium
You are given a 0-indexed m x n binary matrix grid.

Evaluate Division

medium
You 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

medium
You are given an m x n binary matrix grid and an integer health.

Find All Groups of Farmland

medium
You 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

medium
There is a directed graph of n nodes with each node labeled from 0 to n - 1.

Find the Safest Path in a Grid

medium
You 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

medium
You 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

medium
There are n people, each person has a unique id between 0 and n-1.

Grid Teleportation Traversal

medium
You 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?

medium
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.

Jump Game III

medium
Given 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

medium
You 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

medium
There 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

medium
You 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

medium
You are given an integer matrix isWater of size m x n that represents a map of land and water cells.

Max Area of Island

medium
You are given an m x n binary matrix grid.