2025-03-20
3108. Minimum Cost Walk in Weighted Graph
Topic: Array, Bit Manipulation, Union Find, Graph
Difficulty: Hard
Problem:
There is an undirected weighted graph with
You are given the integer
A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It's important to note that a walk may visit the same edge or vertex more than once.
The cost of a walk starting at node
You are also given a 2D array
Return the array
Example 1:
Input: n = 5, edges = [0,1,7,1,3,7,1,2,1], query = [0,3,3,4]
Output: 1,-1
Explanation:
Image: https://assets.leetcode.com/uploads/2024/01/31/q4_example1-1.png
To achieve the cost of 1 in the first query, we need to move on the following edges:
In the second query, there is no walk between nodes 3 and 4, so the answer is -1.
Example 2:
Input: n = 3, edges = [0,2,7,0,1,15,1,2,6,1,2,1], query = [1,2]
Output: 0
Explanation:
Image: https://assets.leetcode.com/uploads/2024/01/31/q4_example2e.png
To achieve the cost of 0 in the first query, we need to move on the following edges:
Constraints:
•
•
•
•
•
•
•
•
•
•
3108. Minimum Cost Walk in Weighted Graph
Topic: Array, Bit Manipulation, Union Find, Graph
Difficulty: Hard
Problem:
There is an undirected weighted graph with
n vertices labeled from 0 to n - 1.You are given the integer
n and an array edges, where edges[i] = [u_i, v_i, w_i] indicates that there is an edge between vertices u_i and v_i with a weight of w_i.A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It's important to note that a walk may visit the same edge or vertex more than once.
The cost of a walk starting at node
u and ending at node v is defined as the bitwise AND of the weights of the edges traversed during the walk. In other words, if the sequence of edge weights encountered during the walk is w_0, w_1, w_2, ..., w_k, then the cost is calculated as w_0 & w_1 & w_2 & ... & w_k, where & denotes the bitwise AND operator.You are also given a 2D array
query, where query[i] = [s_i, t_i]. For each query, you need to find the minimum cost of the walk starting at vertex s_i and ending at vertex t_i. If there exists no such walk, the answer is -1.Return the array
answer, where answer[i] denotes the minimum cost of a walk for query i.Example 1:
Input: n = 5, edges = [0,1,7,1,3,7,1,2,1], query = [0,3,3,4]
Output: 1,-1
Explanation:
Image: https://assets.leetcode.com/uploads/2024/01/31/q4_example1-1.png
To achieve the cost of 1 in the first query, we need to move on the following edges:
0->1 (weight 7), 1->2 (weight 1), 2->1 (weight 1), 1->3 (weight 7).In the second query, there is no walk between nodes 3 and 4, so the answer is -1.
Example 2:
Input: n = 3, edges = [0,2,7,0,1,15,1,2,6,1,2,1], query = [1,2]
Output: 0
Explanation:
Image: https://assets.leetcode.com/uploads/2024/01/31/q4_example2e.png
To achieve the cost of 0 in the first query, we need to move on the following edges:
1->2 (weight 1), 2->1 (weight 6), 1->2 (weight 1).Constraints:
•
2 <= n <= 10^5•
0 <= edges.length <= 10^5•
edges[i].length == 3•
0 <= u_i, v_i <= n - 1•
u_i != v_i•
0 <= w_i <= 10^5•
1 <= query.length <= 10^5•
query[i].length == 2•
0 <= s_i, t_i <= n - 1•
s_i != t_i2025-03-21
2115. Find All Possible Recipes from Given Supplies
Topic: Array, Hash Table, String, Graph, Topological Sort
Difficulty: Medium
Problem:
You have information about
You are also given a string array
Return a list of all the recipes that you can create. You may return the answer in any order.
Note that two recipes may contain each other in their ingredients.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
• All the values of
• Each
2115. Find All Possible Recipes from Given Supplies
Topic: Array, Hash Table, String, Graph, Topological Sort
Difficulty: Medium
Problem:
You have information about
n different recipes. You are given a string array recipes and a 2D string array ingredients. The i^th recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe can also be an ingredient for other recipes, i.e., ingredients[i] may contain a string that is in recipes.You are also given a string array
supplies containing all the ingredients that you initially have, and you have an infinite supply of all of them.Return a list of all the recipes that you can create. You may return the answer in any order.
Note that two recipes may contain each other in their ingredients.
Example 1:
Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
Output: ["bread"]
Explanation:
We can create "bread" since we have the ingredients "yeast" and "flour".
Example 2:
Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
Output: ["bread","sandwich"]
Explanation:
We can create "bread" since we have the ingredients "yeast" and "flour".
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
Example 3:
Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
Output: ["bread","sandwich","burger"]
Explanation:
We can create "bread" since we have the ingredients "yeast" and "flour".
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
Constraints:
•
n == recipes.length == ingredients.length•
1 <= n <= 100•
1 <= ingredients[i].length, supplies.length <= 100•
1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10•
recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.• All the values of
recipes and supplies combined are unique.• Each
ingredients[i] does not contain any duplicate values.2025-03-22
2685. Count the Number of Complete Components
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
You are given an integer
Return the number of complete connected components of the graph.
A connected component is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.
A connected component is said to be complete if there exists an edge between every pair of its vertices.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-31-23.png
Example 2:
Image: https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-32-00.png
Constraints:
•
•
•
•
•
• There are no repeated edges.
2685. Count the Number of Complete Components
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
You are given an integer
n. There is an undirected graph with n vertices, numbered from 0 to n - 1. You are given a 2D integer array edges where edges[i] = [a_i, b_i] denotes that there exists an undirected edge connecting vertices a_i and b_i.Return the number of complete connected components of the graph.
A connected component is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.
A connected component is said to be complete if there exists an edge between every pair of its vertices.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-31-23.png
Input: n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]
Output: 3
Explanation: From the picture above, one can see that all of the components of this graph are complete.
Example 2:
Image: https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-32-00.png
Input: n = 6, edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]
Output: 1
Explanation: The component containing vertices 0, 1, and 2 is complete since there is an edge between every pair of two vertices. On the other hand, the component containing vertices 3, 4, and 5 is not complete since there is no edge between vertices 4 and 5. Thus, the number of complete components in this graph is 1.
Constraints:
•
1 <= n <= 50•
0 <= edges.length <= n * (n - 1) / 2•
edges[i].length == 2•
0 <= a_i, b_i <= n - 1•
a_i != b_i• There are no repeated edges.
2025-03-23
1976. Number of Ways to Arrive at Destination
Topic: Dynamic Programming, Graph, Topological Sort, Shortest Path
Difficulty: Medium
Problem:
You are in a city that consists of
You are given an integer
Return the number of ways you can arrive at your destination in the shortest amount of time. Since the answer may be large, return it modulo
Example 1:
Image: https://assets.leetcode.com/uploads/2025/02/14/1976_corrected.png
Example 2:
Constraints:
•
•
•
•
•
•
• There is at most one road connecting any two intersections.
• You can reach any intersection from any other intersection.
1976. Number of Ways to Arrive at Destination
Topic: Dynamic Programming, Graph, Topological Sort, Shortest Path
Difficulty: Medium
Problem:
You are in a city that consists of
n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections. The inputs are generated such that you can reach any intersection from any other intersection and that there is at most one road between any two intersections.You are given an integer
n and a 2D integer array roads where roads[i] = [u_i, v_i, time_i] means that there is a road between intersections u_i and v_i that takes time_i minutes to travel. You want to know in how many ways you can travel from intersection 0 to intersection n - 1 in the shortest amount of time.Return the number of ways you can arrive at your destination in the shortest amount of time. Since the answer may be large, return it modulo
10^9 + 7.Example 1:
Image: https://assets.leetcode.com/uploads/2025/02/14/1976_corrected.png
Input: n = 7, roads = [[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]]
Output: 4
Explanation: The shortest amount of time it takes to go from intersection 0 to intersection 6 is 7 minutes.
The four ways to get there in 7 minutes are:
- 0 ➝ 6
- 0 ➝ 4 ➝ 6
- 0 ➝ 1 ➝ 2 ➝ 5 ➝ 6
- 0 ➝ 1 ➝ 3 ➝ 5 ➝ 6
Example 2:
Input: n = 2, roads = [[1,0,10]]
Output: 1
Explanation: There is only one way to go from intersection 0 to intersection 1, and it takes 10 minutes.
Constraints:
•
1 <= n <= 200•
n - 1 <= roads.length <= n * (n - 1) / 2•
roads[i].length == 3•
0 <= u_i, v_i <= n - 1•
1 <= time_i <= 10^9•
u_i != v_i• There is at most one road connecting any two intersections.
• You can reach any intersection from any other intersection.
2025-03-24
3169. Count Days Without Meetings
Topic: Array, Sorting
Difficulty: Medium
Problem:
You are given a positive integer
Return the count of days when the employee is available for work but no meetings are scheduled.
Note: The meetings may overlap.
Example 1:
Input: days = 10, meetings = [5,7,1,3,9,10]
Output: 2
Explanation:
There is no meeting scheduled on the 4^th and 8^th days.
Example 2:
Input: days = 5, meetings = [2,4,1,3]
Output: 1
Explanation:
There is no meeting scheduled on the 5^th day.
Example 3:
Input: days = 6, meetings = [1,6]
Output: 0
Explanation:
Meetings are scheduled for all working days.
Constraints:
•
•
•
•
3169. Count Days Without Meetings
Topic: Array, Sorting
Difficulty: Medium
Problem:
You are given a positive integer
days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive).Return the count of days when the employee is available for work but no meetings are scheduled.
Note: The meetings may overlap.
Example 1:
Input: days = 10, meetings = [5,7,1,3,9,10]
Output: 2
Explanation:
There is no meeting scheduled on the 4^th and 8^th days.
Example 2:
Input: days = 5, meetings = [2,4,1,3]
Output: 1
Explanation:
There is no meeting scheduled on the 5^th day.
Example 3:
Input: days = 6, meetings = [1,6]
Output: 0
Explanation:
Meetings are scheduled for all working days.
Constraints:
•
1 <= days <= 10^9•
1 <= meetings.length <= 10^5•
meetings[i].length == 2•
1 <= meetings[i][0] <= meetings[i][1] <= days2025-03-25
3394. Check if Grid can be Cut into Sections
Topic: Array, Sorting
Difficulty: Medium
Problem:
You are given an integer
•
•
Note that the rectangles do not overlap. Your task is to determine if it is possible to make either two horizontal or two vertical cuts on the grid such that:
• Each of the three resulting sections formed by the cuts contains at least one rectangle.
• Every rectangle belongs to exactly one section.
Return
Example 1:
Input: n = 5, rectangles = [1,0,5,2,0,2,2,4,3,2,5,3,0,4,4,5]
Output: true
Explanation:
Image: https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png
The grid is shown in the diagram. We can make horizontal cuts at
Example 2:
Input: n = 4, rectangles = [0,0,1,1,2,0,3,4,0,2,2,3,3,0,4,3]
Output: true
Explanation:
Image: https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png
We can make vertical cuts at
Example 3:
Input: n = 4, rectangles = [0,2,2,4,1,0,3,2,2,2,3,4,3,0,4,2,3,2,4,4]
Output: false
Explanation:
We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.
Constraints:
•
•
•
•
• No two rectangles overlap.
3394. Check if Grid can be Cut into Sections
Topic: Array, Sorting
Difficulty: Medium
Problem:
You are given an integer
n representing the dimensions of an n x n grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates rectangles, where rectangles[i] is in the form [start_x, start_y, end_x, end_y], representing a rectangle on the grid. Each rectangle is defined as follows:•
(start_x, start_y): The bottom-left corner of the rectangle.•
(end_x, end_y): The top-right corner of the rectangle.Note that the rectangles do not overlap. Your task is to determine if it is possible to make either two horizontal or two vertical cuts on the grid such that:
• Each of the three resulting sections formed by the cuts contains at least one rectangle.
• Every rectangle belongs to exactly one section.
Return
true if such cuts can be made; otherwise, return false.Example 1:
Input: n = 5, rectangles = [1,0,5,2,0,2,2,4,3,2,5,3,0,4,4,5]
Output: true
Explanation:
Image: https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png
The grid is shown in the diagram. We can make horizontal cuts at
y = 2 and y = 4. Hence, output is true.Example 2:
Input: n = 4, rectangles = [0,0,1,1,2,0,3,4,0,2,2,3,3,0,4,3]
Output: true
Explanation:
Image: https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png
We can make vertical cuts at
x = 2 and x = 3. Hence, output is true.Example 3:
Input: n = 4, rectangles = [0,2,2,4,1,0,3,2,2,2,3,4,3,0,4,2,3,2,4,4]
Output: false
Explanation:
We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.
Constraints:
•
3 <= n <= 10^9•
3 <= rectangles.length <= 10^5•
0 <= rectangles[i][0] < rectangles[i][2] <= n•
0 <= rectangles[i][1] < rectangles[i][3] <= n• No two rectangles overlap.
2025-03-26
2033. Minimum Operations to Make a Uni-Value Grid
Topic: Array, Math, Sorting, Matrix
Difficulty: Medium
Problem:
You are given a 2D integer
A uni-value grid is a grid where all the elements of it are equal.
Return the minimum number of operations to make the grid uni-value. If it is not possible, return
Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/21/gridtxt.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/21/gridtxt-1.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/09/21/gridtxt-2.png
Constraints:
•
•
•
•
•
2033. Minimum Operations to Make a Uni-Value Grid
Topic: Array, Math, Sorting, Matrix
Difficulty: Medium
Problem:
You are given a 2D integer
grid of size m x n and an integer x. In one operation, you can add x to or subtract x from any element in the grid.A uni-value grid is a grid where all the elements of it are equal.
Return the minimum number of operations to make the grid uni-value. If it is not possible, return
-1.Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/21/gridtxt.png
Input: grid = [[2,4],[6,8]], x = 2
Output: 4
Explanation: We can make every element equal to 4 by doing the following:
- Add x to 2 once.
- Subtract x from 6 once.
- Subtract x from 8 twice.
A total of 4 operations were used.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/21/gridtxt-1.png
Input: grid = [[1,5],[2,3]], x = 1
Output: 5
Explanation: We can make every element equal to 3.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/09/21/gridtxt-2.png
Input: grid = [[1,2],[3,4]], x = 2
Output: -1
Explanation: It is impossible to make every element equal.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 10^5•
1 <= m * n <= 10^5•
1 <= x, grid[i][j] <= 10^42025-03-27
2780. Minimum Index of a Valid Split
Topic: Array, Hash Table, Sorting
Difficulty: Medium
Problem:
An element
You are given a 0-indexed integer array
You can split
•
•
Here,
Return the minimum index of a valid split. If no valid split exists, return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2780. Minimum Index of a Valid Split
Topic: Array, Hash Table, Sorting
Difficulty: Medium
Problem:
An element
x of an integer array arr of length m is dominant if more than half the elements of arr have a value of x.You are given a 0-indexed integer array
nums of length n with one dominant element.You can split
nums at an index i into two arrays nums[0, ..., i] and nums[i + 1, ..., n - 1], but the split is only valid if:•
0 <= i < n - 1•
nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element.Here,
nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j, both ends being inclusive. Particularly, if j < i then nums[i, ..., j] denotes an empty subarray.Return the minimum index of a valid split. If no valid split exists, return
-1.Example 1:
Input: nums = [1,2,2,2]
Output: 2
Explanation: We can split the array at index 2 to obtain arrays [1,2,2] and [2].
In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3.
In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1.
Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split.
It can be shown that index 2 is the minimum index of a valid split.
Example 2:
Input: nums = [2,1,3,1,1,1,7,1,2,1]
Output: 4
Explanation: We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1].
In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split.
It can be shown that index 4 is the minimum index of a valid split.
Example 3:
Input: nums = [3,3,3,3,7,2,2]
Output: -1
Explanation: It can be shown that there is no valid split.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^9•
nums has exactly one dominant element.2025-03-28
2503. Maximum Number of Points From Grid Queries
Topic: Array, Two Pointers, Breadth-First Search, Union Find, Sorting, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
You are given an
Find an array
• If
• Otherwise, you do not get any points, and you end this process.
After the process,
Return the resulting array
Example 1:
Image: https://assets.leetcode.com/uploads/2025/03/15/image1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/10/20/yetgriddrawio-2.png
Constraints:
•
•
•
•
•
•
•
2503. Maximum Number of Points From Grid Queries
Topic: Array, Two Pointers, Breadth-First Search, Union Find, Sorting, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
You are given an
m x n integer matrix grid and an array queries of size k.Find an array
answer of size k such that for each integer queries[i] you start in the top left cell of the matrix and repeat the following process:• If
queries[i] is strictly greater than the value of the current cell that you are in, then you get one point if it is your first time visiting this cell, and you can move to any adjacent cell in all 4 directions: up, down, left, and right.• Otherwise, you do not get any points, and you end this process.
After the process,
answer[i] is the maximum number of points you can get. Note that for each query you are allowed to visit the same cell multiple times.Return the resulting array
answer.Example 1:
Image: https://assets.leetcode.com/uploads/2025/03/15/image1.png
Input: grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]
Output: [5,8,1]
Explanation: The diagrams above show which cells we visit to get points for each query.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/10/20/yetgriddrawio-2.png
Input: grid = [[5,2,1],[1,1,2]], queries = [3]
Output: [0]
Explanation: We can not get any points because the value of the top left cell is already greater than or equal to 3.
Constraints:
•
m == grid.length•
n == grid[i].length•
2 <= m, n <= 1000•
4 <= m * n <= 10^5•
k == queries.length•
1 <= k <= 10^4•
1 <= grid[i][j], queries[i] <= 10^62025-03-29
2818. Apply Operations to Maximize Score
Topic: Array, Math, Stack, Greedy, Sorting, Monotonic Stack, Number Theory
Difficulty: Hard
Problem:
You are given an array
Initially, you start with a score of
• Choose any non-empty subarray
• Choose an element
• Multiply your score by
Here,
The prime score of an integer
Return the maximum possible score after applying at most
Since the answer may be large, return it modulo
Example 1:
Example 2:
Constraints:
•
•
•
2818. Apply Operations to Maximize Score
Topic: Array, Math, Stack, Greedy, Sorting, Monotonic Stack, Number Theory
Difficulty: Hard
Problem:
You are given an array
nums of n positive integers and an integer k.Initially, you start with a score of
1. You have to maximize your score by applying the following operation at most k times:• Choose any non-empty subarray
nums[l, ..., r] that you haven't chosen previously.• Choose an element
x of nums[l, ..., r] with the highest prime score. If multiple such elements exist, choose the one with the smallest index.• Multiply your score by
x.Here,
nums[l, ..., r] denotes the subarray of nums starting at index l and ending at the index r, both ends being inclusive.The prime score of an integer
x is equal to the number of distinct prime factors of x. For example, the prime score of 300 is 3 since 300 = 2 * 2 * 3 * 5 * 5.Return the maximum possible score after applying at most
k operations.Since the answer may be large, return it modulo
10^9 + 7.Example 1:
Input: nums = [8,3,9,3,8], k = 2
Output: 81
Explanation: To get a score of 81, we can apply the following operations:
- Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9.
- Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81.
It can be proven that 81 is the highest score one can obtain.
Example 2:
Input: nums = [19,12,14,6,10,18], k = 3
Output: 4788
Explanation: To get a score of 4788, we can apply the following operations:
- Choose subarray nums[0, ..., 0]. nums[0] is the only element in this subarray. Hence, we multiply the score by nums[0]. The score becomes 1 * 19 = 19.
- Choose subarray nums[5, ..., 5]. nums[5] is the only element in this subarray. Hence, we multiply the score by nums[5]. The score becomes 19 * 18 = 342.
- Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 2, but nums[2] has the smaller index. Hence, we multipy the score by nums[2]. The score becomes 342 * 14 = 4788.
It can be proven that 4788 is the highest score one can obtain.
Constraints:
•
1 <= nums.length == n <= 10^5•
1 <= nums[i] <= 10^5•
1 <= k <= min(n * (n + 1) / 2, 10^9)2025-03-30
763. Partition Labels
Topic: Hash Table, Two Pointers, String, Greedy
Difficulty: Medium
Problem:
You are given a string
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be
Return a list of integers representing the size of these parts.
Example 1:
Example 2:
Constraints:
•
•
763. Partition Labels
Topic: Hash Table, Two Pointers, String, Greedy
Difficulty: Medium
Problem:
You are given a string
s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string "ababcc" can be partitioned into ["abab", "cc"], but partitions such as ["aba", "bcc"] or ["ab", "ab", "cc"] are invalid.Note that the partition is done so that after concatenating all the parts in order, the resultant string should be
s.Return a list of integers representing the size of these parts.
Example 1:
Input: s = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
Example 2:
Input: s = "eccbbbbdec"
Output: [10]
Constraints:
•
1 <= s.length <= 500•
s consists of lowercase English letters.2025-03-31
2551. Put Marbles in Bags
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
You have
Divide the marbles into the
• No bag is empty.
• If the
• If a bag consists of all the marbles with an index from
The score after distributing the marbles is the sum of the costs of all the
Return the difference between the maximum and minimum scores among marble distributions.
Example 1:
Example 2:
Constraints:
•
•
2551. Put Marbles in Bags
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
You have
k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the i^th marble. You are also given the integer k.Divide the marbles into the
k bags according to the following rules:• No bag is empty.
• If the
i^th marble and j^th marble are in a bag, then all marbles with an index between the i^th and j^th indices should also be in that same bag.• If a bag consists of all the marbles with an index from
i to j inclusively, then the cost of the bag is weights[i] + weights[j].The score after distributing the marbles is the sum of the costs of all the
k bags.Return the difference between the maximum and minimum scores among marble distributions.
Example 1:
Input: weights = [1,3,5,1], k = 2
Output: 4
Explanation:
The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6.
The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10.
Thus, we return their difference 10 - 6 = 4.
Example 2:
Input: weights = [1, 3], k = 2
Output: 0
Explanation: The only distribution possible is [1],[3].
Since both the maximal and minimal score are the same, we return 0.
Constraints:
•
1 <= k <= weights.length <= 10^5•
1 <= weights[i] <= 10^92025-04-01
2140. Solving Questions With Brainpower
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given a 0-indexed 2D integer array
The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question
• For example, given
• If question
• If instead, question
Return the maximum points you can earn for the exam.
Example 1:
Example 2:
Constraints:
•
•
•
2140. Solving Questions With Brainpower
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given a 0-indexed 2D integer array
questions where questions[i] = [points_i, brainpower_i].The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question
0) and make a decision whether to solve or skip each question. Solving question i will earn you points_i points but you will be unable to solve each of the next brainpower_i questions. If you skip question i, you get to make the decision on the next question.• For example, given
questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:• If question
0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.• If instead, question
0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.Return the maximum points you can earn for the exam.
Example 1:
Input: questions = [[3,2],[4,3],[4,4],[2,5]]
Output: 5
Explanation: The maximum points can be earned by solving questions 0 and 3.
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
- Unable to solve questions 1 and 2
- Solve question 3: Earn 2 points
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
Example 2:
Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
Output: 7
Explanation: The maximum points can be earned by solving questions 1 and 4.
- Skip question 0
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
- Unable to solve questions 2 and 3
- Solve question 4: Earn 5 points
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
Constraints:
•
1 <= questions.length <= 10^5•
questions[i].length == 2•
1 <= points_i, brainpower_i <= 10^52025-04-02
2873. Maximum Value of an Ordered Triplet I
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
Return the maximum value over all triplets of indices
The value of a triplet of indices
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2873. Maximum Value of an Ordered Triplet I
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
nums.Return the maximum value over all triplets of indices
(i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.The value of a triplet of indices
(i, j, k) is equal to (nums[i] - nums[j]) * nums[k].Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
•
3 <= nums.length <= 100•
1 <= nums[i] <= 10^62025-04-03
2874. Maximum Value of an Ordered Triplet II
Topic: Array
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
Return the maximum value over all triplets of indices
The value of a triplet of indices
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2874. Maximum Value of an Ordered Triplet II
Topic: Array
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums.Return the maximum value over all triplets of indices
(i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.The value of a triplet of indices
(i, j, k) is equal to (nums[i] - nums[j]) * nums[k].Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
•
3 <= nums.length <= 10^5•
1 <= nums[i] <= 10^62025-04-04
1123. Lowest Common Ancestor of Deepest Leaves
Topic: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Recall that:
• The node of a binary tree is a leaf if and only if it has no children
• The depth of the root of the tree is
• The lowest common ancestor of a set
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png
Example 2:
Example 3:
Constraints:
• The number of nodes in the tree will be in the range
•
• The values of the nodes in the tree are unique.
Note: This question is the same as 865: <https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/>
1123. Lowest Common Ancestor of Deepest Leaves
Topic: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return the lowest common ancestor of its deepest leaves.Recall that:
• The node of a binary tree is a leaf if and only if it has no children
• The depth of the root of the tree is
0. if the depth of a node is d, the depth of each of its children is d + 1.• The lowest common ancestor of a set
S of nodes, is the node A with the largest depth such that every node in S is in the subtree with root A.Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png
Input: root = [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation: We return the node with value 2, colored in yellow in the diagram.
The nodes coloured in blue are the deepest leaf-nodes of the tree.
Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.
Example 2:
Input: root = [1]
Output: [1]
Explanation: The root is the deepest node in the tree, and it's the lca of itself.
Example 3:
Input: root = [0,1,3,null,2]
Output: [2]
Explanation: The deepest leaf node in the tree is 2, the lca of one node is itself.
Constraints:
• The number of nodes in the tree will be in the range
[1, 1000].•
0 <= Node.val <= 1000• The values of the nodes in the tree are unique.
Note: This question is the same as 865: <https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/>
2025-04-05
1863. Sum of All Subset XOR Totals
Topic: Array, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration
Difficulty: Easy
Problem:
The XOR total of an array is defined as the bitwise
• For example, the XOR total of the array
Given an array
Note: Subsets with the same elements should be counted multiple times.
An array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1863. Sum of All Subset XOR Totals
Topic: Array, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration
Difficulty: Easy
Problem:
The XOR total of an array is defined as the bitwise
XOR of all its elements, or 0 if the array is empty.• For example, the XOR total of the array
[2,5,6] is 2 XOR 5 XOR 6 = 1.Given an array
nums, return the sum of all XOR totals for every subset of nums. Note: Subsets with the same elements should be counted multiple times.
An array
a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.Example 1:
Input: nums = [1,3]
Output: 6
Explanation: The 4 subsets of [1,3] are:
- The empty subset has an XOR total of 0.
- [1] has an XOR total of 1.
- [3] has an XOR total of 3.
- [1,3] has an XOR total of 1 XOR 3 = 2.
0 + 1 + 3 + 2 = 6
Example 2:
Input: nums = [5,1,6]
Output: 28
Explanation: The 8 subsets of [5,1,6] are:
- The empty subset has an XOR total of 0.
- [5] has an XOR total of 5.
- [1] has an XOR total of 1.
- [6] has an XOR total of 6.
- [5,1] has an XOR total of 5 XOR 1 = 4.
- [5,6] has an XOR total of 5 XOR 6 = 3.
- [1,6] has an XOR total of 1 XOR 6 = 7.
- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.
0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
Example 3:
Input: nums = [3,4,5,6,7,8]
Output: 480
Explanation: The sum of all XOR totals for every subset is 480.
Constraints:
•
1 <= nums.length <= 12•
1 <= nums[i] <= 202025-04-06
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
•
•
If there are multiple solutions, return any of them.
Example 1:
Example 2:
Constraints:
•
•
• All the integers in
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:•
answer[i] % answer[j] == 0, or•
answer[j] % answer[i] == 0If there are multiple solutions, return any of them.
Example 1:
Input: nums = [1,2,3]
Output: [1,2]
Explanation: [1,3] is also accepted.
Example 2:
Input: nums = [1,2,4,8]
Output: [1,2,4,8]
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= 2 * 10^9• All the integers in
nums are unique.2025-04-07
416. Partition Equal Subset Sum
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
416. Partition Equal Subset Sum
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.Example 1:
Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
•
1 <= nums.length <= 200•
1 <= nums[i] <= 1002025-04-08
3396. Minimum Number of Operations to Make Elements in Array Distinct
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
• Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.
Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.
Example 1:
Input: nums = 1,2,3,4,2,3,3,5,7
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
• In the second operation, the next 3 elements are removed, resulting in the array
Therefore, the answer is 2.
Example 2:
Input: nums = 4,5,6,4,4
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
• In the second operation, all remaining elements are removed, resulting in an empty array.
Therefore, the answer is 2.
Example 3:
Input: nums = 6,7,8,9
Output: 0
Explanation:
The array already contains distinct elements. Therefore, the answer is 0.
Constraints:
•
•
3396. Minimum Number of Operations to Make Elements in Array Distinct
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:• Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.
Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.
Example 1:
Input: nums = 1,2,3,4,2,3,3,5,7
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
[4, 2, 3, 3, 5, 7].• In the second operation, the next 3 elements are removed, resulting in the array
[3, 5, 7], which has distinct elements.Therefore, the answer is 2.
Example 2:
Input: nums = 4,5,6,4,4
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
[4, 4].• In the second operation, all remaining elements are removed, resulting in an empty array.
Therefore, the answer is 2.
Example 3:
Input: nums = 6,7,8,9
Output: 0
Explanation:
The array already contains distinct elements. Therefore, the answer is 0.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 1002025-04-09
3375. Minimum Operations to Make Array Values Equal to K
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
An integer
For example, if
You are allowed to perform the following operation on
• Select an integer
• For each index
Return the minimum number of operations required to make every element in
Example 1:
Input: nums = 5,2,5,4,5, k = 2
Output: 2
Explanation:
The operations can be performed in order using valid integers 4 and then 2.
Example 2:
Input: nums = 2,1,2, k = 2
Output: -1
Explanation:
It is impossible to make all the values equal to 2.
Example 3:
Input: nums = 9,7,5,3, k = 1
Output: 4
Explanation:
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
Constraints:
•
•
•
3375. Minimum Operations to Make Array Values Equal to K
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
nums and an integer k.An integer
h is called valid if all values in the array that are strictly greater than h are identical.For example, if
nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 are equal to 10, but 5 is not a valid integer.You are allowed to perform the following operation on
nums:• Select an integer
h that is valid for the current values in nums.• For each index
i where nums[i] > h, set nums[i] to h.Return the minimum number of operations required to make every element in
nums equal to k. If it is impossible to make all elements equal to k, return -1.Example 1:
Input: nums = 5,2,5,4,5, k = 2
Output: 2
Explanation:
The operations can be performed in order using valid integers 4 and then 2.
Example 2:
Input: nums = 2,1,2, k = 2
Output: -1
Explanation:
It is impossible to make all the values equal to 2.
Example 3:
Input: nums = 9,7,5,3, k = 1
Output: 4
Explanation:
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 100•
1 <= k <= 100