2025-01-20
2661. First Completely Painted Row or Column
Topic: Array, Hash Table, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
Go through each index
Return the smallest index
Example 1:
Image: image explanation for example 1
Image: https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2023/01/18/grid2.jpg
Constraints:
•
•
•
•
•
•
• All the integers of
• All the integers of
2661. First Completely Painted Row or Column
Topic: Array, Hash Table, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
arr, and an m x n integer matrix mat. arr and mat both contain all the integers in the range [1, m * n].Go through each index
i in arr starting from index 0 and paint the cell in mat containing the integer arr[i].Return the smallest index
i at which either a row or a column will be completely painted in mat.Example 1:
Image: image explanation for example 1
Image: https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg
Input: arr = [1,3,4,2], mat = [[1,4],[2,3]]
Output: 2
Explanation: The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2].
Example 2:
Image: https://assets.leetcode.com/uploads/2023/01/18/grid2.jpg
Input: arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
Output: 3
Explanation: The second column becomes fully painted at arr[3].
Constraints:
•
m == mat.length•
n = mat[i].length•
arr.length == m * n•
1 <= m, n <= 10^5•
1 <= m * n <= 10^5•
1 <= arr[i], mat[r][c] <= m * n• All the integers of
arr are unique.• All the integers of
mat are unique.2025-01-21
2017. Grid Game
Topic: Array, Matrix, Prefix Sum
Difficulty: Medium
Problem:
You are given a 0-indexed 2D array
Both robots initially start at
At the start of the game, the first robot moves from
The first robot wants to minimize the number of points collected by the second robot. In contrast, the second robot wants to maximize the number of points it collects. If both robots play optimally, return the number of points collected by the second robot.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/08/a1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/08/a2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/09/08/a3.png
Constraints:
•
•
•
•
2017. Grid Game
Topic: Array, Matrix, Prefix Sum
Difficulty: Medium
Problem:
You are given a 0-indexed 2D array
grid of size 2 x n, where grid[r][c] represents the number of points at position (r, c) on the matrix. Two robots are playing a game on this matrix.Both robots initially start at
(0, 0) and want to reach (1, n-1). Each robot may only move to the right ((r, c) to (r, c + 1)) or down ((r, c) to (r + 1, c)).At the start of the game, the first robot moves from
(0, 0) to (1, n-1), collecting all the points from the cells on its path. For all cells (r, c) traversed on the path, grid[r][c] is set to 0. Then, the second robot moves from (0, 0) to (1, n-1), collecting the points on its path. Note that their paths may intersect with one another.The first robot wants to minimize the number of points collected by the second robot. In contrast, the second robot wants to maximize the number of points it collects. If both robots play optimally, return the number of points collected by the second robot.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/08/a1.png
Input: grid = [[2,5,4],[1,5,1]]
Output: 4
Explanation: The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 0 + 4 + 0 = 4 points.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/08/a2.png
Input: grid = [[3,3,1],[8,5,2]]
Output: 4
Explanation: The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 3 + 1 + 0 = 4 points.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/09/08/a3.png
Input: grid = [[1,3,1,15],[1,3,3,1]]
Output: 7
Explanation: The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.
Constraints:
•
grid.length == 2•
n == grid[r].length•
1 <= n <= 5 * 10^4•
1 <= grid[r][c] <= 10^52025-01-22
1765. Map of Highest Peak
Topic: Array, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given an integer matrix
• If
• If
You must assign each cell a height in a way that follows these rules:
• The height of each cell must be non-negative.
• If the cell is a water cell, its height must be
• Any two adjacent cells must have an absolute height difference of at most
Find an assignment of heights such that the maximum height in the matrix is maximized.
Return an integer matrix
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82045-am.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82050-am.png
Constraints:
•
•
•
•
• There is at least one water cell.
Note: This question is the same as 542: https://leetcode.com/problems/01-matrix/
1765. Map of Highest Peak
Topic: Array, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given an integer matrix
isWater of size m x n that represents a map of land and water cells.• If
isWater[i][j] == 0, cell (i, j) is a land cell.• If
isWater[i][j] == 1, cell (i, j) is a water cell.You must assign each cell a height in a way that follows these rules:
• The height of each cell must be non-negative.
• If the cell is a water cell, its height must be
0.• Any two adjacent cells must have an absolute height difference of at most
1. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).Find an assignment of heights such that the maximum height in the matrix is maximized.
Return an integer matrix
height of size m x n where height[i][j] is cell (i, j)'s height. If there are multiple solutions, return any of them.Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82045-am.png
Input: isWater = [[0,1],[0,0]]
Output: [[1,0],[2,1]]
Explanation: The image shows the assigned heights of each cell.
The blue cell is the water cell, and the green cells are the land cells.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82050-am.png
Input: isWater = [[0,0,1],[1,0,0],[0,0,0]]
Output: [[1,1,0],[0,1,1],[1,2,2]]
Explanation: A height of 2 is the maximum possible height of any assignment.
Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
Constraints:
•
m == isWater.length•
n == isWater[i].length•
1 <= m, n <= 1000•
isWater[i][j] is 0 or 1.• There is at least one water cell.
Note: This question is the same as 542: https://leetcode.com/problems/01-matrix/
2025-01-23
1267. Count Servers that Communicate
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix, Counting
Difficulty: Medium
Problem:
You are given a map of a server center, represented as a
Return the number of servers that communicate with any other server.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-6.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2019/11/13/untitled-diagram-4.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-1-3.jpg
Constraints:
•
•
•
•
•
1267. Count Servers that Communicate
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix, Counting
Difficulty: Medium
Problem:
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. Two servers are said to communicate if they are on the same row or on the same column.Return the number of servers that communicate with any other server.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-6.jpg
Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.
Example 2:
Image: https://assets.leetcode.com/uploads/2019/11/13/untitled-diagram-4.jpg
Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.
Example 3:
Image: https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-1-3.jpg
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m <= 250•
1 <= n <= 250•
grid[i][j] == 0 or 12025-01-24
802. Find Eventual Safe States
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There is a directed graph of
A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another safe node).
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png
Example 2:
Constraints:
•
•
•
•
•
• The graph may contain self-loops.
• The number of edges in the graph will be in the range
802. Find Eventual Safe States
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There is a directed graph of
n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in graph[i].A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another safe node).
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Explanation: The given graph is shown above.
Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
Example 2:
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
Output: [4]
Explanation:
Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
Constraints:
•
n == graph.length•
1 <= n <= 10^4•
0 <= graph[i].length <= n•
0 <= graph[i][j] <= n - 1•
graph[i] is sorted in a strictly increasing order.• The graph may contain self-loops.
• The number of edges in the graph will be in the range
[1, 4 * 10^4].2025-01-25
2948. Make Lexicographically Smallest Array by Swapping Elements
Topic: Array, Union Find, Sorting
Difficulty: Medium
Problem:
You are given a 0-indexed array of positive integers
In one operation, you can choose any two indices
Return the lexicographically smallest array that can be obtained by performing the operation any number of times.
An array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2948. Make Lexicographically Smallest Array by Swapping Elements
Topic: Array, Union Find, Sorting
Difficulty: Medium
Problem:
You are given a 0-indexed array of positive integers
nums and a positive integer limit.In one operation, you can choose any two indices
i and j and swap nums[i] and nums[j] if |nums[i] - nums[j]| <= limit.Return the lexicographically smallest array that can be obtained by performing the operation any number of times.
An array
a is lexicographically smaller than an array b if in the first position where a and b differ, array a has an element that is less than the corresponding element in b. For example, the array [2,10,3] is lexicographically smaller than the array [10,2,3] because they differ at index 0 and 2 < 10.Example 1:
Input: nums = [1,5,3,9,8], limit = 2
Output: [1,3,5,8,9]
Explanation: Apply the operation 2 times:
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
We cannot obtain a lexicographically smaller array by applying any more operations.
Note that it may be possible to get the same result by doing different operations.
Example 2:
Input: nums = [1,7,6,18,2,1], limit = 3
Output: [1,6,7,18,1,2]
Explanation: Apply the operation 3 times:
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
We cannot obtain a lexicographically smaller array by applying any more operations.
Example 3:
Input: nums = [1,7,28,19,10], limit = 3
Output: [1,7,28,19,10]
Explanation: [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^9•
1 <= limit <= 10^92025-01-26
2127. Maximum Employees to Be Invited to a Meeting
Topic: Depth-First Search, Graph, Topological Sort
Difficulty: Hard
Problem:
A company is organizing a meeting and has a list of
The employees are numbered from
Given a 0-indexed integer array
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/14/ex1.png
Example 2:
Example 3:
Image: https://assets.leetcode.com/uploads/2021/12/14/ex2.png
Constraints:
•
•
•
•
2127. Maximum Employees to Be Invited to a Meeting
Topic: Depth-First Search, Graph, Topological Sort
Difficulty: Hard
Problem:
A company is organizing a meeting and has a list of
n employees, waiting to be invited. They have arranged for a large circular table, capable of seating any number of employees.The employees are numbered from
0 to n - 1. Each employee has a favorite person and they will attend the meeting only if they can sit next to their favorite person at the table. The favorite person of an employee is not themself.Given a 0-indexed integer array
favorite, where favorite[i] denotes the favorite person of the i^th employee, return the maximum number of employees that can be invited to the meeting.Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/14/ex1.png
Input: favorite = [2,2,1,2]
Output: 3
Explanation:
The above figure shows how the company can invite employees 0, 1, and 2, and seat them at the round table.
All employees cannot be invited because employee 2 cannot sit beside employees 0, 1, and 3, simultaneously.
Note that the company can also invite employees 1, 2, and 3, and give them their desired seats.
The maximum number of employees that can be invited to the meeting is 3.
Example 2:
Input: favorite = [1,2,0]
Output: 3
Explanation:
Each employee is the favorite person of at least one other employee, and the only way the company can invite them is if they invite every employee.
The seating arrangement will be the same as that in the figure given in example 1:
- Employee 0 will sit between employees 2 and 1.
- Employee 1 will sit between employees 0 and 2.
- Employee 2 will sit between employees 1 and 0.
The maximum number of employees that can be invited to the meeting is 3.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/12/14/ex2.png
Input: favorite = [3,0,1,4,1]
Output: 4
Explanation:
The above figure shows how the company will invite employees 0, 1, 3, and 4, and seat them at the round table.
Employee 2 cannot be invited because the two spots next to their favorite employee 1 are taken.
So the company leaves them out of the meeting.
The maximum number of employees that can be invited to the meeting is 4.
Constraints:
•
n == favorite.length•
2 <= n <= 10^5•
0 <= favorite[i] <= n - 1•
favorite[i] != i2025-01-27
1462. Course Schedule IV
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There are a total of
• For example, the pair
Prerequisites can also be indirect. If course
You are also given an array
Return a boolean array
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/01/courses4-1-graph.jpg
Example 2:
Example 3:
Image: https://assets.leetcode.com/uploads/2021/05/01/courses4-3-graph.jpg
Constraints:
•
•
•
•
•
• All the pairs
• The prerequisites graph has no cycles.
•
•
•
1462. Course Schedule IV
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There are a total of
numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [a_i, b_i] indicates that you must take course a_i first if you want to take course b_i.• For example, the pair
[0, 1] indicates that you have to take course 0 before you can take course 1.Prerequisites can also be indirect. If course
a is a prerequisite of course b, and course b is a prerequisite of course c, then course a is a prerequisite of course c.You are also given an array
queries where queries[j] = [u_j, v_j]. For the j^th query, you should answer whether course u_j is a prerequisite of course v_j or not.Return a boolean array
answer, where answer[j] is the answer to the j^th query.Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/01/courses4-1-graph.jpg
Input: numCourses = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]]
Output: [false,true]
Explanation: The pair [1, 0] indicates that you have to take course 1 before you can take course 0.
Course 0 is not a prerequisite of course 1, but the opposite is true.
Example 2:
Input: numCourses = 2, prerequisites = [], queries = [[1,0],[0,1]]
Output: [false,false]
Explanation: There are no prerequisites, and each course is independent.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/05/01/courses4-3-graph.jpg
Input: numCourses = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]]
Output: [true,true]
Constraints:
•
2 <= numCourses <= 100•
0 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2)•
prerequisites[i].length == 2•
0 <= a_i, b_i <= numCourses - 1•
a_i != b_i• All the pairs
[a_i, b_i] are unique.• The prerequisites graph has no cycles.
•
1 <= queries.length <= 10^4•
0 <= u_i, v_i <= numCourses - 1•
u_i != v_i2025-01-28
2658. Maximum Number of Fish in a Grid
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed 2D matrix
• A land cell if
• A water cell containing
A fisher can start at any water cell
• Catch all the fish at cell
• Move to any adjacent water cell.
Return the maximum number of fish the fisher can catch if he chooses his starting cell optimally, or
An adjacent cell of the cell
Example 1:
Image: https://assets.leetcode.com/uploads/2023/03/29/example.png
Example 2:
Image: https://assets.leetcode.com/uploads/2023/03/29/example2.png
Constraints:
•
•
•
•
2658. Maximum Number of Fish in a Grid
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed 2D matrix
grid of size m x n, where (r, c) represents:• A land cell if
grid[r][c] = 0, or• A water cell containing
grid[r][c] fish, if grid[r][c] > 0.A fisher can start at any water cell
(r, c) and can do the following operations any number of times:• Catch all the fish at cell
(r, c), or• Move to any adjacent water cell.
Return the maximum number of fish the fisher can catch if he chooses his starting cell optimally, or
0 if no water cell exists.An adjacent cell of the cell
(r, c), is one of the cells (r, c + 1), (r, c - 1), (r + 1, c) or (r - 1, c) if it exists.Example 1:
Image: https://assets.leetcode.com/uploads/2023/03/29/example.png
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
Output: 7
Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.
Example 2:
Image: https://assets.leetcode.com/uploads/2023/03/29/example2.png
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
Output: 1
Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 10•
0 <= grid[i][j] <= 102025-01-29
684. Redundant Connection
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
In this problem, a tree is an undirected graph that is connected and has no cycles.
You are given a graph that started as a tree with
Return an edge that can be removed so that the resulting graph is a tree of
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/02/reduntant1-2-graph.jpg
Constraints:
•
•
•
•
•
• There are no repeated edges.
• The given graph is connected.
684. Redundant Connection
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
In this problem, a tree is an undirected graph that is connected and has no cycles.
You are given a graph that started as a tree with
n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the graph.Return an edge that can be removed so that the resulting graph is a tree of
n nodes. If there are multiple answers, return the answer that occurs last in the input.Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.jpg
Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/02/reduntant1-2-graph.jpg
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]
Constraints:
•
n == edges.length•
3 <= n <= 1000•
edges[i].length == 2•
1 <= a_i < b_i <= edges.length•
a_i != b_i• There are no repeated edges.
• The given graph is connected.
2025-01-30
2493. Divide Nodes Into the Maximum Number of Groups
Topic: Breadth-First Search, Union Find, Graph
Difficulty: Hard
Problem:
You are given a positive integer
You are also given a 2D integer array
Divide the nodes of the graph into
• Each node in the graph belongs to exactly one group.
• For every pair of nodes in the graph that are connected by an edge
Return the maximum number of groups (i.e., maximum
Example 1:
Image: https://assets.leetcode.com/uploads/2022/10/13/example1.png
Example 2:
Constraints:
•
•
•
•
•
• There is at most one edge between any pair of vertices.
2493. Divide Nodes Into the Maximum Number of Groups
Topic: Breadth-First Search, Union Find, Graph
Difficulty: Hard
Problem:
You are given a positive integer
n representing the number of nodes in an undirected graph. The nodes are labeled from 1 to n.You are also given a 2D integer array
edges, where edges[i] = [a_i, b_i] indicates that there is a bidirectional edge between nodes a_i and b_i. Notice that the given graph may be disconnected.Divide the nodes of the graph into
m groups (1-indexed) such that:• Each node in the graph belongs to exactly one group.
• For every pair of nodes in the graph that are connected by an edge
[a_i, b_i], if a_i belongs to the group with index x, and b_i belongs to the group with index y, then |y - x| = 1.Return the maximum number of groups (i.e., maximum
m) into which you can divide the nodes. Return -1 if it is impossible to group the nodes with the given conditions.Example 1:
Image: https://assets.leetcode.com/uploads/2022/10/13/example1.png
Input: n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]
Output: 4
Explanation: As shown in the image we:
- Add node 5 to the first group.
- Add node 1 to the second group.
- Add nodes 2 and 4 to the third group.
- Add nodes 3 and 6 to the fourth group.
We can see that every edge is satisfied.
It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied.
Example 2:
Input: n = 3, edges = [[1,2],[2,3],[3,1]]
Output: -1
Explanation: If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied.
It can be shown that no grouping is possible.
Constraints:
•
1 <= n <= 500•
1 <= edges.length <= 10^4•
edges[i].length == 2•
1 <= a_i, b_i <= n•
a_i != b_i• There is at most one edge between any pair of vertices.
2025-01-31
827. Making A Large Island
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Hard
Problem:
You are given an
Return the size of the largest island in
An island is a 4-directionally connected group of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
827. Making A Large Island
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Hard
Problem:
You are given an
n x n binary matrix grid. You are allowed to change at most one 0 to be 1.Return the size of the largest island in
grid after applying this operation.An island is a 4-directionally connected group of
1s.Example 1:
Input: grid = [[1,0],[0,1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
Example 2:
Input: grid = [[1,1],[1,0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
Example 3:
Input: grid = [[1,1],[1,1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 4.
Constraints:
•
n == grid.length•
n == grid[i].length•
1 <= n <= 500•
grid[i][j] is either 0 or 1.2025-02-01
3151. Special Array I
Topic: Array
Difficulty: Easy
Problem:
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integers
Example 1:
Input: nums = 1
Output: true
Explanation:
There is only one element. So the answer is
Example 2:
Input: nums = 2,1,4
Output: true
Explanation:
There is only two pairs:
Example 3:
Input: nums = 4,3,1,6
Output: false
Explanation:
Constraints:
•
•
3151. Special Array I
Topic: Array
Difficulty: Easy
Problem:
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integers
nums. Return true if nums is a special array, otherwise, return false.Example 1:
Input: nums = 1
Output: true
Explanation:
There is only one element. So the answer is
true.Example 2:
Input: nums = 2,1,4
Output: true
Explanation:
There is only two pairs:
(2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.Example 3:
Input: nums = 4,3,1,6
Output: false
Explanation:
nums[1] and nums[2] are both odd. So the answer is false.Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 1002025-02-02
1752. Check if Array Is Sorted and Rotated
Topic: Array
Difficulty: Easy
Problem:
Given an array
There may be duplicates in the original array.
Note: An array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1752. Check if Array Is Sorted and Rotated
Topic: Array
Difficulty: Easy
Problem:
Given an array
nums, return true if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false.There may be duplicates in the original array.
Note: An array
A rotated by x positions results in an array B of the same length such that A[i] == B[(i+x) % A.length], where % is the modulo operation.Example 1:
Input: nums = [3,4,5,1,2]
Output: true
Explanation: [1,2,3,4,5] is the original sorted array.
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].
Example 2:
Input: nums = [2,1,3,4]
Output: false
Explanation: There is no sorted array once rotated that can make nums.
Example 3:
Input: nums = [1,2,3]
Output: true
Explanation: [1,2,3] is the original sorted array.
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 1002025-02-03
3105. Longest Strictly Increasing or Strictly Decreasing Subarray
Topic: Array
Difficulty: Easy
Problem:
You are given an array of integers
Example 1:
Input: nums = 1,4,3,3,2
Output: 2
Explanation:
The strictly increasing subarrays of
The strictly decreasing subarrays of
Hence, we return
Example 2:
Input: nums = 3,3,3,3
Output: 1
Explanation:
The strictly increasing subarrays of
The strictly decreasing subarrays of
Hence, we return
Example 3:
Input: nums = 3,2,1
Output: 3
Explanation:
The strictly increasing subarrays of
The strictly decreasing subarrays of
Hence, we return
Constraints:
•
•
3105. Longest Strictly Increasing or Strictly Decreasing Subarray
Topic: Array
Difficulty: Easy
Problem:
You are given an array of integers
nums. Return the length of the longest subarray of nums which is either strictly increasing or strictly decreasing.Example 1:
Input: nums = 1,4,3,3,2
Output: 2
Explanation:
The strictly increasing subarrays of
nums are [1], [2], [3], [3], [4], and [1,4].The strictly decreasing subarrays of
nums are [1], [2], [3], [3], [4], [3,2], and [4,3].Hence, we return
2.Example 2:
Input: nums = 3,3,3,3
Output: 1
Explanation:
The strictly increasing subarrays of
nums are [3], [3], [3], and [3].The strictly decreasing subarrays of
nums are [3], [3], [3], and [3].Hence, we return
1.Example 3:
Input: nums = 3,2,1
Output: 3
Explanation:
The strictly increasing subarrays of
nums are [3], [2], and [1].The strictly decreasing subarrays of
nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].Hence, we return
3.Constraints:
•
1 <= nums.length <= 50•
1 <= nums[i] <= 502025-02-04
1800. Maximum Ascending Subarray Sum
Topic: Array
Difficulty: Easy
Problem:
Given an array of positive integers
A subarray is defined as a contiguous sequence of numbers in an array.
A subarray
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1800. Maximum Ascending Subarray Sum
Topic: Array
Difficulty: Easy
Problem:
Given an array of positive integers
nums, return the maximum possible sum of an ascending subarray in nums.A subarray is defined as a contiguous sequence of numbers in an array.
A subarray
[nums_l, nums_l+1, ..., nums_r-1, nums_r] is ascending if for all i where l <= i < r, nums_i < nums_i+1. Note that a subarray of size 1 is ascending.Example 1:
Input: nums = [10,20,30,5,10,50]
Output: 65
Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.
Example 2:
Input: nums = [10,20,30,40,50]
Output: 150
Explanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.
Example 3:
Input: nums = [12,17,15,13,10,11,12]
Output: 33
Explanation: [10,11,12] is the ascending subarray with the maximum sum of 33.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 1002025-02-05
1790. Check if One String Swap Can Make Strings Equal
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
You are given two strings
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1790. Check if One String Swap Can Make Strings Equal
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
You are given two strings
s1 and s2 of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.Return
true if it is possible to make both strings equal by performing at most one string swap on exactly one of the strings. Otherwise, return false.Example 1:
Input: s1 = "bank", s2 = "kanb"
Output: true
Explanation: For example, swap the first character with the last character of s2 to make "bank".
Example 2:
Input: s1 = "attack", s2 = "defend"
Output: false
Explanation: It is impossible to make them equal with one string swap.
Example 3:
Input: s1 = "kelb", s2 = "kelb"
Output: true
Explanation: The two strings are already equal, so no string swap operation is required.
Constraints:
•
1 <= s1.length, s2.length <= 100•
s1.length == s2.length•
s1 and s2 consist of only lowercase English letters.2025-02-06
1726. Tuple with Same Product
Topic: Array, Hash Table, Counting
Difficulty: Medium
Problem:
Given an array
Example 1:
Example 2:
Constraints:
•
•
• All elements in
1726. Tuple with Same Product
Topic: Array, Hash Table, Counting
Difficulty: Medium
Problem:
Given an array
nums of distinct positive integers, return the number of tuples (a, b, c, d) such that a * b = c * d where a, b, c, and d are elements of nums, and a != b != c != d.Example 1:
Input: nums = [2,3,4,6]
Output: 8
Explanation: There are 8 valid tuples:
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
Example 2:
Input: nums = [1,2,4,5,10]
Output: 16
Explanation: There are 16 valid tuples:
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= 10^4• All elements in
nums are distinct.2025-02-07
3160. Find the Number of Distinct Colors Among the Balls
Topic: Array, Hash Table, Simulation
Difficulty: Medium
Problem:
You are given an integer
There are
Return an array
Note that when answering a query, lack of a color will not be considered as a color.
Example 1:
Input: limit = 4, queries = [1,4,2,5,1,3,3,4]
Output: 1,2,2,3
Explanation:
Image: https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif
• After query 0, ball 1 has color 4.
• After query 1, ball 1 has color 4, and ball 2 has color 5.
• After query 2, ball 1 has color 3, and ball 2 has color 5.
• After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.
Example 2:
Input: limit = 4, queries = [0,1,1,2,2,2,3,4,4,5]
Output: 1,2,2,3,4
Explanation:
Image: https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif
• After query 0, ball 0 has color 1.
• After query 1, ball 0 has color 1, and ball 1 has color 2.
• After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.
• After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.
• After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5.
Constraints:
•
•
•
•
•
3160. Find the Number of Distinct Colors Among the Balls
Topic: Array, Hash Table, Simulation
Difficulty: Medium
Problem:
You are given an integer
limit and a 2D array queries of size n x 2.There are
limit + 1 balls with distinct labels in the range [0, limit]. Initially, all balls are uncolored. For every query in queries that is of the form [x, y], you mark ball x with the color y. After each query, you need to find the number of distinct colors among the balls.Return an array
result of length n, where result[i] denotes the number of distinct colors after i^th query.Note that when answering a query, lack of a color will not be considered as a color.
Example 1:
Input: limit = 4, queries = [1,4,2,5,1,3,3,4]
Output: 1,2,2,3
Explanation:
Image: https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif
• After query 0, ball 1 has color 4.
• After query 1, ball 1 has color 4, and ball 2 has color 5.
• After query 2, ball 1 has color 3, and ball 2 has color 5.
• After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.
Example 2:
Input: limit = 4, queries = [0,1,1,2,2,2,3,4,4,5]
Output: 1,2,2,3,4
Explanation:
Image: https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif
• After query 0, ball 0 has color 1.
• After query 1, ball 0 has color 1, and ball 1 has color 2.
• After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.
• After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.
• After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5.
Constraints:
•
1 <= limit <= 10^9•
1 <= n == queries.length <= 10^5•
queries[i].length == 2•
0 <= queries[i][0] <= limit•
1 <= queries[i][1] <= 10^92025-02-08
2349. Design a Number Container System
Topic: Hash Table, Design, Heap (Priority Queue), Ordered Set
Difficulty: Medium
Problem:
Design a number container system that can do the following:
• Insert or Replace a number at the given index in the system.
• Return the smallest index for the given number in the system.
Implement the
•
•
•
Example 1:
Constraints:
•
• At most
2349. Design a Number Container System
Topic: Hash Table, Design, Heap (Priority Queue), Ordered Set
Difficulty: Medium
Problem:
Design a number container system that can do the following:
• Insert or Replace a number at the given index in the system.
• Return the smallest index for the given number in the system.
Implement the
NumberContainers class:•
NumberContainers() Initializes the number container system.•
void change(int index, int number) Fills the container at index with the number. If there is already a number at that index, replace it.•
int find(int number) Returns the smallest index for the given number, or -1 if there is no index that is filled by number in the system.Example 1:
Input
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
Output
[null, -1, null, null, null, null, 1, null, 2]
Explanation
NumberContainers nc = new NumberContainers();
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
Constraints:
•
1 <= index, number <= 10^9• At most
10^5 calls will be made in total to change and find.