2025-01-08
3042. Count Prefix and Suffix Pairs I
Topic: Array, String, Trie, Rolling Hash, String Matching, Hash Function
Difficulty: Easy
Problem:
You are given a 0-indexed string array
Let's define a boolean function
•
For example,
Return an integer denoting the number of index pairs
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
3042. Count Prefix and Suffix Pairs I
Topic: Array, String, Trie, Rolling Hash, String Matching, Hash Function
Difficulty: Easy
Problem:
You are given a 0-indexed string array
words.Let's define a boolean function
isPrefixAndSuffix that takes two strings, str1 and str2:•
isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of str2, and false otherwise.For example,
isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.Return an integer denoting the number of index pairs
(i, j) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.Example 1:
Input: words = ["a","aba","ababa","aa"]
Output: 4
Explanation: In this example, the counted index pairs are:
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
Therefore, the answer is 4.
Example 2:
Input: words = ["pa","papa","ma","mama"]
Output: 2
Explanation: In this example, the counted index pairs are:
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
Therefore, the answer is 2.
Example 3:
Input: words = ["abab","ab"]
Output: 0
Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
Therefore, the answer is 0.
Constraints:
•
1 <= words.length <= 50•
1 <= words[i].length <= 10•
words[i] consists only of lowercase English letters.2025-01-09
2185. Counting Words With a Given Prefix
Topic: Array, String, String Matching
Difficulty: Easy
Problem:
You are given an array of strings
Return the number of strings in
A prefix of a string
Example 1:
Example 2:
Constraints:
•
•
•
2185. Counting Words With a Given Prefix
Topic: Array, String, String Matching
Difficulty: Easy
Problem:
You are given an array of strings
words and a string pref.Return the number of strings in
words that contain pref as a prefix.A prefix of a string
s is any leading contiguous substring of s.Example 1:
Input: words = ["pay","attention","practice","attend"], pref = "at"
Output: 2
Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
Example 2:
Input: words = ["leetcode","win","loops","success"], pref = "code"
Output: 0
Explanation: There are no strings that contain "code" as a prefix.
Constraints:
•
1 <= words.length <= 100•
1 <= words[i].length, pref.length <= 100•
words[i] and pref consist of lowercase English letters.2025-01-10
916. Word Subsets
Topic: Array, Hash Table, String
Difficulty: Medium
Problem:
You are given two string arrays
A string
• For example,
A string
Return an array of all the universal strings in
Example 1:
Example 2:
Constraints:
•
•
•
• All the strings of
916. Word Subsets
Topic: Array, Hash Table, String
Difficulty: Medium
Problem:
You are given two string arrays
words1 and words2.A string
b is a subset of string a if every letter in b occurs in a including multiplicity.• For example,
"wrr" is a subset of "warrior" but is not a subset of "world".A string
a from words1 is universal if for every string b in words2, b is a subset of a.Return an array of all the universal strings in
words1. You may return the answer in any order.Example 1:
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]
Example 2:
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]
Constraints:
•
1 <= words1.length, words2.length <= 10^4•
1 <= words1[i].length, words2[i].length <= 10•
words1[i] and words2[i] consist only of lowercase English letters.• All the strings of
words1 are unique.2025-01-11
1400. Construct K Palindrome Strings
Topic: Hash Table, String, Greedy, Counting
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1400. Construct K Palindrome Strings
Topic: Hash Table, String, Greedy, Counting
Difficulty: Medium
Problem:
Given a string
s and an integer k, return true if you can use all the characters in s to construct k palindrome strings or false otherwise.Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Constraints:
•
1 <= s.length <= 10^5•
s consists of lowercase English letters.•
1 <= k <= 10^52025-01-12
2116. Check if a Parentheses String Can Be Valid
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
A parentheses string is a non-empty string consisting only of
• It is
• It can be written as
• It can be written as
You are given a parentheses string
• If
• But if
Return
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/06/eg1.png
Example 2:
Example 3:
Constraints:
•
•
•
•
2116. Check if a Parentheses String Can Be Valid
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
A parentheses string is a non-empty string consisting only of
'(' and ')'. It is valid if any of the following conditions is true:• It is
().• It can be written as
AB (A concatenated with B), where A and B are valid parentheses strings.• It can be written as
(A), where A is a valid parentheses string.You are given a parentheses string
s and a string locked, both of length n. locked is a binary string consisting only of '0's and '1's. For each index i of locked,• If
locked[i] is '1', you cannot change s[i].• But if
locked[i] is '0', you can change s[i] to either '(' or ')'.Return
true if you can make s a valid parentheses string. Otherwise, return false.Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/06/eg1.png
Input: s = "))()))", locked = "010100"
Output: true
Explanation: locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3].
We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid.
Example 2:
Input: s = "()()", locked = "0000"
Output: true
Explanation: We do not need to make any changes because s is already valid.
Example 3:
Input: s = ")", locked = "0"
Output: false
Explanation: locked permits us to change s[0].
Changing s[0] to either '(' or ')' will not make s valid.
Constraints:
•
n == s.length == locked.length•
1 <= n <= 10^5•
s[i] is either '(' or ')'.•
locked[i] is either '0' or '1'.2025-01-13
3223. Minimum Length of String After Operations
Topic: Hash Table, String, Counting
Difficulty: Medium
Problem:
You are given a string
You can perform the following process on
• Choose an index
• Delete the closest character to the left of index
• Delete the closest character to the right of index
Return the minimum length of the final string
Example 1:
Input: s = "abaacbcbb"
Output: 5
Explanation:
We do the following operations:
• Choose index 2, then remove the characters at indices 0 and 3. The resulting string is
• Choose index 3, then remove the characters at indices 0 and 5. The resulting string is
Example 2:
Input: s = "aa"
Output: 2
Explanation:
We cannot perform any operations, so we return the length of the original string.
Constraints:
•
•
3223. Minimum Length of String After Operations
Topic: Hash Table, String, Counting
Difficulty: Medium
Problem:
You are given a string
s.You can perform the following process on
s any number of times:• Choose an index
i in the string such that there is at least one character to the left of index i that is equal to s[i], and at least one character to the right that is also equal to s[i].• Delete the closest character to the left of index
i that is equal to s[i].• Delete the closest character to the right of index
i that is equal to s[i].Return the minimum length of the final string
s that you can achieve.Example 1:
Input: s = "abaacbcbb"
Output: 5
Explanation:
We do the following operations:
• Choose index 2, then remove the characters at indices 0 and 3. The resulting string is
s = "bacbcbb".• Choose index 3, then remove the characters at indices 0 and 5. The resulting string is
s = "acbcb".Example 2:
Input: s = "aa"
Output: 2
Explanation:
We cannot perform any operations, so we return the length of the original string.
Constraints:
•
1 <= s.length <= 2 * 10^5•
s consists only of lowercase English letters.2025-01-14
2657. Find the Prefix Common Array of Two Arrays
Topic: Array, Hash Table, Bit Manipulation
Difficulty: Medium
Problem:
You are given two 0-indexed integer permutations
A prefix common array of
Return the prefix common array of
A sequence of
Example 1:
Example 2:
Constraints:
•
•
•
2657. Find the Prefix Common Array of Two Arrays
Topic: Array, Hash Table, Bit Manipulation
Difficulty: Medium
Problem:
You are given two 0-indexed integer permutations
A and B of length n.A prefix common array of
A and B is an array C such that C[i] is equal to the count of numbers that are present at or before the index i in both A and B.Return the prefix common array of
A and B.A sequence of
n integers is called a permutation if it contains all integers from 1 to n exactly once.Example 1:
Input: A = [1,3,2,4], B = [3,1,2,4]
Output: [0,2,3,4]
Explanation: At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
Example 2:
Input: A = [2,3,1], B = [3,1,2]
Output: [0,1,3]
Explanation: At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
Constraints:
•
1 <= A.length == B.length == n <= 50•
1 <= A[i], B[i] <= n•
It is guaranteed that A and B are both a permutation of n integers.2025-01-15
2429. Minimize XOR
Topic: Greedy, Bit Manipulation
Difficulty: Medium
Problem:
Given two positive integers
•
• The value
Note that
Return the integer
The number of set bits of an integer is the number of
Example 1:
Example 2:
Constraints:
•
2429. Minimize XOR
Topic: Greedy, Bit Manipulation
Difficulty: Medium
Problem:
Given two positive integers
num1 and num2, find the positive integer x such that:•
x has the same number of set bits as num2, and• The value
x XOR num1 is minimal.Note that
XOR is the bitwise XOR operation.Return the integer
x. The test cases are generated such that x is uniquely determined.The number of set bits of an integer is the number of
1's in its binary representation.Example 1:
Input: num1 = 3, num2 = 5
Output: 3
Explanation:
The binary representations of num1 and num2 are 0011 and 0101, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.
Example 2:
Input: num1 = 1, num2 = 12
Output: 3
Explanation:
The binary representations of num1 and num2 are 0001 and 1100, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.
Constraints:
•
1 <= num1, num2 <= 10^92025-01-16
2425. Bitwise XOR of All Pairings
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given two 0-indexed arrays,
Return the bitwise XOR of all integers in
Example 1:
Example 2:
Constraints:
•
•
2425. Bitwise XOR of All Pairings
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given two 0-indexed arrays,
nums1 and nums2, consisting of non-negative integers. There exists another array, nums3, which contains the bitwise XOR of all pairings of integers between nums1 and nums2 (every integer in nums1 is paired with every integer in nums2 exactly once).Return the bitwise XOR of all integers in
nums3.Example 1:
Input: nums1 = [2,1,3], nums2 = [10,2,5,0]
Output: 13
Explanation:
A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
The bitwise XOR of all these numbers is 13, so we return 13.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 0
Explanation:
All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],
and nums1[1] ^ nums2[1].
Thus, one possible nums3 array is [2,5,1,6].
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.
Constraints:
•
1 <= nums1.length, nums2.length <= 10^5•
0 <= nums1[i], nums2[j] <= 10^92025-01-17
2683. Neighboring Bitwise XOR
Topic: Array, Bit Manipulation
Difficulty: Medium
Problem:
A 0-indexed array
Specifically, for each index
• If
• Otherwise,
Given an array
Return true if such an array exists or false otherwise.
• A binary array is an array containing only 0's and 1's
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• The values in
2683. Neighboring Bitwise XOR
Topic: Array, Bit Manipulation
Difficulty: Medium
Problem:
A 0-indexed array
derived with length n is derived by computing the bitwise XOR (⊕) of adjacent values in a binary array original of length n.Specifically, for each index
i in the range [0, n - 1]:• If
i = n - 1, then derived[i] = original[i] ⊕ original[0].• Otherwise,
derived[i] = original[i] ⊕ original[i + 1].Given an array
derived, your task is to determine whether there exists a valid binary array original that could have formed derived.Return true if such an array exists or false otherwise.
• A binary array is an array containing only 0's and 1's
Example 1:
Input: derived = [1,1,0]
Output: true
Explanation: A valid original array that gives derived is [0,1,0].
derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1
derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1
derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0
Example 2:
Input: derived = [1,1]
Output: true
Explanation: A valid original array that gives derived is [0,1].
derived[0] = original[0] ⊕ original[1] = 1
derived[1] = original[1] ⊕ original[0] = 1
Example 3:
Input: derived = [1,0]
Output: false
Explanation: There is no valid original array that gives derived.
Constraints:
•
n == derived.length•
1 <= n <= 10^5• The values in
derived are either 0's or 1's2025-01-18
1368. Minimum Cost to Make at Least One Valid Path in a Grid
Topic: Array, Breadth-First Search, Graph, Heap (Priority Queue), Matrix, Shortest Path
Difficulty: Hard
Problem:
Given an
•
•
•
•
Notice that there could be some signs on the cells of the grid that point outside the grid.
You will initially start at the upper left cell
You can modify the sign on a cell with
Return the minimum cost to make the grid have at least one valid path.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/02/13/grid1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/02/13/grid2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2020/02/13/grid3.png
Constraints:
•
•
•
•
1368. Minimum Cost to Make at Least One Valid Path in a Grid
Topic: Array, Breadth-First Search, Graph, Heap (Priority Queue), Matrix, Shortest Path
Difficulty: Hard
Problem:
Given an
m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be:•
1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1])•
2 which means go to the cell to the left. (i.e go from grid[i][j] to grid[i][j - 1])•
3 which means go to the lower cell. (i.e go from grid[i][j] to grid[i + 1][j])•
4 which means go to the upper cell. (i.e go from grid[i][j] to grid[i - 1][j])Notice that there could be some signs on the cells of the grid that point outside the grid.
You will initially start at the upper left cell
(0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1) following the signs on the grid. The valid path does not have to be the shortest.You can modify the sign on a cell with
cost = 1. You can modify the sign on a cell one time only.Return the minimum cost to make the grid have at least one valid path.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/02/13/grid1.png
Input: grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
Output: 3
Explanation: You will start at point (0, 0).
The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
The total cost = 3.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/02/13/grid2.png
Input: grid = [[1,1,3],[3,2,2],[1,1,4]]
Output: 0
Explanation: You can follow the path from (0, 0) to (2, 2).
Example 3:
Image: https://assets.leetcode.com/uploads/2020/02/13/grid3.png
Input: grid = [[1,2],[4,3]]
Output: 1
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 100•
1 <= grid[i][j] <= 42025-01-19
407. Trapping Rain Water II
Topic: Array, Breadth-First Search, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
Given an
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg
Constraints:
•
•
•
•
407. Trapping Rain Water II
Topic: Array, Breadth-First Search, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
Given an
m x n integer matrix heightMap representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining.Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg
Input: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
Output: 4
Explanation: After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg
Input: heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
Output: 10
Constraints:
•
m == heightMap.length•
n == heightMap[i].length•
1 <= m, n <= 200•
0 <= heightMap[i][j] <= 2 * 10^42025-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_i