2025-11-01
3217. Delete Nodes From Linked List Present in Array
Topic: Array, Hash Table, Linked List
Difficulty: Medium
Problem:
You are given an array of integers
Example 1:
Input: nums = 1,2,3, head = 1,2,3,4,5
Output: 4,5
Explanation:
Image: https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png
Remove the nodes with values 1, 2, and 3.
Example 2:
Input: nums = 1, head = 1,2,1,2,1,2
Output: 2,2,2
Explanation:
Image: https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png
Remove the nodes with value 1.
Example 3:
Input: nums = 5, head = 1,2,3,4
Output: 1,2,3,4
Explanation:
Image: https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png
No node has value 5.
Constraints:
•
•
• All elements in
• The number of nodes in the given list is in the range
•
• The input is generated such that there is at least one node in the linked list that has a value not present in
3217. Delete Nodes From Linked List Present in Array
Topic: Array, Hash Table, Linked List
Difficulty: Medium
Problem:
You are given an array of integers
nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums.Example 1:
Input: nums = 1,2,3, head = 1,2,3,4,5
Output: 4,5
Explanation:
Image: https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png
Remove the nodes with values 1, 2, and 3.
Example 2:
Input: nums = 1, head = 1,2,1,2,1,2
Output: 2,2,2
Explanation:
Image: https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png
Remove the nodes with value 1.
Example 3:
Input: nums = 5, head = 1,2,3,4
Output: 1,2,3,4
Explanation:
Image: https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png
No node has value 5.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^5• All elements in
nums are unique.• The number of nodes in the given list is in the range
[1, 10^5].•
1 <= Node.val <= 10^5• The input is generated such that there is at least one node in the linked list that has a value not present in
nums.2025-11-02
2257. Count Unguarded Cells in the Grid
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
You are given two integers
A guard can see every cell in the four cardinal directions (north, east, south, or west) starting from their position unless obstructed by a wall or another guard. A cell is guarded if there is at least one guard that can see it.
Return the number of unoccupied cells that are not guarded.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png
Constraints:
•
•
•
•
•
•
•
• All the positions in
2257. Count Unguarded Cells in the Grid
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
You are given two integers
m and n representing a 0-indexed m x n grid. You are also given two 2D integer arrays guards and walls where guards[i] = [row_i, col_i] and walls[j] = [row_j, col_j] represent the positions of the i^th guard and j^th wall respectively.A guard can see every cell in the four cardinal directions (north, east, south, or west) starting from their position unless obstructed by a wall or another guard. A cell is guarded if there is at least one guard that can see it.
Return the number of unoccupied cells that are not guarded.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png
Input: m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
Output: 7
Explanation: The guarded and unguarded cells are shown in red and green respectively in the above diagram.
There are a total of 7 unguarded cells, so we return 7.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png
Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
Output: 4
Explanation: The unguarded cells are shown in green in the above diagram.
There are a total of 4 unguarded cells, so we return 4.
Constraints:
•
1 <= m, n <= 10^5•
2 <= m * n <= 10^5•
1 <= guards.length, walls.length <= 5 * 10^4•
2 <= guards.length + walls.length <= m * n•
guards[i].length == walls[j].length == 2•
0 <= row_i, row_j < m•
0 <= col_i, col_j < n• All the positions in
guards and walls are unique.2025-11-03
1578. Minimum Time to Make Rope Colorful
Topic: Array, String, Dynamic Programming, Greedy
Difficulty: Medium
Problem:
Alice has
Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array
Return the minimum time Bob needs to make the rope colorful.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg
Constraints:
•
•
•
•
1578. Minimum Time to Make Rope Colorful
Topic: Array, String, Dynamic Programming, Greedy
Difficulty: Medium
Problem:
Alice has
n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the i^th balloon.Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array
neededTime where neededTime[i] is the time (in seconds) that Bob needs to remove the i^th balloon from the rope.Return the minimum time Bob needs to make the rope colorful.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg
Input: colors = "abaac", neededTime = [1,2,3,4,5]
Output: 3
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
Bob can remove the blue balloon at index 2. This takes 3 seconds.
There are no longer two consecutive balloons of the same color. Total time = 3.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg
Input: colors = "abc", neededTime = [1,2,3]
Output: 0
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Explanation: Bob will remove the balloons at indices 0 and 4. Each balloons takes 1 second to remove.
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
Constraints:
•
n == colors.length == neededTime.length•
1 <= n <= 10^5•
1 <= neededTime[i] <= 10^4•
colors contains only lowercase English letters.2025-11-04
3318. Find X-Sum of All K-Long Subarrays I
Topic: Array, Hash Table, Sliding Window, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array
The x-sum of an array is calculated by the following procedure:
• Count the occurrences of all elements in the array.
• Keep only the occurrences of the top
• Calculate the sum of the resulting array.
Note that if an array has less than
Return an integer array
Example 1:
Input: nums = 1,1,2,2,3,4,2,3, k = 6, x = 2
Output: 6,10,12
Explanation:
• For subarray
• For subarray
• For subarray
Example 2:
Input: nums = 3,8,7,8,7,5, k = 2, x = 2
Output: 11,15,15,15,12
Explanation:
Since
Constraints:
•
•
•
3318. Find X-Sum of All K-Long Subarrays I
Topic: Array, Hash Table, Sliding Window, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array
nums of n integers and two integers k and x.The x-sum of an array is calculated by the following procedure:
• Count the occurrences of all elements in the array.
• Keep only the occurrences of the top
x most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.• Calculate the sum of the resulting array.
Note that if an array has less than
x distinct elements, its x-sum is the sum of the array.Return an integer array
answer of length n - k + 1 where answer[i] is the x-sum of the subarray nums[i..i + k - 1].Example 1:
Input: nums = 1,1,2,2,3,4,2,3, k = 6, x = 2
Output: 6,10,12
Explanation:
• For subarray
[1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.• For subarray
[1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.• For subarray
[2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.Example 2:
Input: nums = 3,8,7,8,7,5, k = 2, x = 2
Output: 11,15,15,15,12
Explanation:
Since
k == x, answer[i] is equal to the sum of the subarray nums[i..i + k - 1].Constraints:
•
1 <= n == nums.length <= 50•
1 <= nums[i] <= 50•
1 <= x <= k <= nums.length2025-11-05
3321. Find X-Sum of All K-Long Subarrays II
Topic: Array, Hash Table, Sliding Window, Heap (Priority Queue)
Difficulty: Hard
Problem:
You are given an array
The x-sum of an array is calculated by the following procedure:
• Count the occurrences of all elements in the array.
• Keep only the occurrences of the top
• Calculate the sum of the resulting array.
Note that if an array has less than
Return an integer array
Example 1:
Input: nums = 1,1,2,2,3,4,2,3, k = 6, x = 2
Output: 6,10,12
Explanation:
• For subarray
• For subarray
• For subarray
Example 2:
Input: nums = 3,8,7,8,7,5, k = 2, x = 2
Output: 11,15,15,15,12
Explanation:
Since
Constraints:
•
•
•
•
3321. Find X-Sum of All K-Long Subarrays II
Topic: Array, Hash Table, Sliding Window, Heap (Priority Queue)
Difficulty: Hard
Problem:
You are given an array
nums of n integers and two integers k and x.The x-sum of an array is calculated by the following procedure:
• Count the occurrences of all elements in the array.
• Keep only the occurrences of the top
x most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.• Calculate the sum of the resulting array.
Note that if an array has less than
x distinct elements, its x-sum is the sum of the array.Return an integer array
answer of length n - k + 1 where answer[i] is the x-sum of the subarray nums[i..i + k - 1].Example 1:
Input: nums = 1,1,2,2,3,4,2,3, k = 6, x = 2
Output: 6,10,12
Explanation:
• For subarray
[1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.• For subarray
[1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.• For subarray
[2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.Example 2:
Input: nums = 3,8,7,8,7,5, k = 2, x = 2
Output: 11,15,15,15,12
Explanation:
Since
k == x, answer[i] is equal to the sum of the subarray nums[i..i + k - 1].Constraints:
•
nums.length == n•
1 <= n <= 10^5•
1 <= nums[i] <= 10^9•
1 <= x <= k <= nums.length2025-11-06
3607. Power Grid Maintenance
Topic: Array, Hash Table, Depth-First Search, Breadth-First Search, Union Find, Graph, Heap (Priority Queue), Ordered Set
Difficulty: Medium
Problem:
You are given an integer
These stations are interconnected via
Initially, all stations are online (operational).
You are also given a 2D array
•
•
Return an array of integers representing the results of each query of type
Note: The power grid preserves its structure; an offline (non‑operational) node remains part of its grid and taking it offline does not alter connectivity.
Example 1:
Input: c = 5, connections = [1,2,2,3,3,4,4,5], queries = [1,3,2,1,1,1,2,2,1,2]
Output: 3,2,3
Explanation:
Image: https://assets.leetcode.com/uploads/2025/04/15/powergrid.jpg
• Initially, all stations
• Query
• Query
• Query
• Query
• Query
Example 2:
Input: c = 3, connections = , queries = [1,1,2,1,1,1]
Output: 1,-1
Explanation:
• There are no connections, so each station is its own isolated grid.
• Query
• Query
• Query
Constraints:
•
•
•
•
•
•
•
•
•
3607. Power Grid Maintenance
Topic: Array, Hash Table, Depth-First Search, Breadth-First Search, Union Find, Graph, Heap (Priority Queue), Ordered Set
Difficulty: Medium
Problem:
You are given an integer
c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing).These stations are interconnected via
n bidirectional cables, represented by a 2D array connections, where each element connections[i] = [u_i, v_i] indicates a connection between station u_i and station v_i. Stations that are directly or indirectly connected form a power grid.Initially, all stations are online (operational).
You are also given a 2D array
queries, where each query is one of the following two types:•
[1, x]: A maintenance check is requested for station x. If station x is online, it resolves the check by itself. If station x is offline, the check is resolved by the operational station with the smallest id in the same power grid as x. If no operational station exists in that grid, return -1.•
[2, x]: Station x goes offline (i.e., it becomes non-operational).Return an array of integers representing the results of each query of type
[1, x] in the order they appear.Note: The power grid preserves its structure; an offline (non‑operational) node remains part of its grid and taking it offline does not alter connectivity.
Example 1:
Input: c = 5, connections = [1,2,2,3,3,4,4,5], queries = [1,3,2,1,1,1,2,2,1,2]
Output: 3,2,3
Explanation:
Image: https://assets.leetcode.com/uploads/2025/04/15/powergrid.jpg
• Initially, all stations
{1, 2, 3, 4, 5} are online and form a single power grid.• Query
[1,3]: Station 3 is online, so the maintenance check is resolved by station 3.• Query
[2,1]: Station 1 goes offline. The remaining online stations are {2, 3, 4, 5}.• Query
[1,1]: Station 1 is offline, so the check is resolved by the operational station with the smallest id among {2, 3, 4, 5}, which is station 2.• Query
[2,2]: Station 2 goes offline. The remaining online stations are {3, 4, 5}.• Query
[1,2]: Station 2 is offline, so the check is resolved by the operational station with the smallest id among {3, 4, 5}, which is station 3.Example 2:
Input: c = 3, connections = , queries = [1,1,2,1,1,1]
Output: 1,-1
Explanation:
• There are no connections, so each station is its own isolated grid.
• Query
[1,1]: Station 1 is online in its isolated grid, so the maintenance check is resolved by station 1.• Query
[2,1]: Station 1 goes offline.• Query
[1,1]: Station 1 is offline and there are no other stations in its grid, so the result is -1.Constraints:
•
1 <= c <= 10^5•
0 <= n == connections.length <= min(10^5, c * (c - 1) / 2)•
connections[i].length == 2•
1 <= u_i, v_i <= c•
u_i != v_i•
1 <= queries.length <= 2 * 10^5•
queries[i].length == 2•
queries[i][0] is either 1 or 2.•
1 <= queries[i][1] <= c2025-11-07
2528. Maximize the Minimum Powered City
Topic: Array, Binary Search, Greedy, Queue, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
Each power station can provide power to every city in a fixed range. In other words, if the range is denoted by
• Note that
The power of a city is the total number of power stations it is being provided power from.
The government has sanctioned building
Given the two integers
Note that you can build the
Example 1:
Example 2:
Constraints:
•
•
•
•
•
2528. Maximize the Minimum Powered City
Topic: Array, Binary Search, Greedy, Queue, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
stations of length n, where stations[i] represents the number of power stations in the i^th city.Each power station can provide power to every city in a fixed range. In other words, if the range is denoted by
r, then a power station at city i can provide power to all cities j such that |i - j| <= r and 0 <= i, j <= n - 1.• Note that
|x| denotes absolute value. For example, |7 - 5| = 2 and |3 - 10| = 7.The power of a city is the total number of power stations it is being provided power from.
The government has sanctioned building
k more power stations, each of which can be built in any city, and have the same range as the pre-existing ones.Given the two integers
r and k, return the maximum possible minimum power of a city, if the additional power stations are built optimally.Note that you can build the
k power stations in multiple cities.Example 1:
Input: stations = [1,2,4,5,0], r = 1, k = 2
Output: 5
Explanation:
One of the optimal ways is to install both the power stations at city 1.
So stations will become [1,4,4,5,0].
- City 0 is provided by 1 + 4 = 5 power stations.
- City 1 is provided by 1 + 4 + 4 = 9 power stations.
- City 2 is provided by 4 + 4 + 5 = 13 power stations.
- City 3 is provided by 5 + 4 = 9 power stations.
- City 4 is provided by 5 + 0 = 5 power stations.
So the minimum power of a city is 5.
Since it is not possible to obtain a larger power, we return 5.
Example 2:
Input: stations = [4,4,4,4], r = 0, k = 3
Output: 4
Explanation:
It can be proved that we cannot make the minimum power of a city greater than 4.
Constraints:
•
n == stations.length•
1 <= n <= 10^5•
0 <= stations[i] <= 10^5•
0 <= r <= n - 1•
0 <= k <= 10^92025-11-08
1611. Minimum One Bit Operations to Make Integers Zero
Topic: Dynamic Programming, Bit Manipulation, Memoization
Difficulty: Hard
Problem:
Given an integer
• Change the rightmost (
• Change the
Return the minimum number of operations to transform
Example 1:
Example 2:
Constraints:
•
1611. Minimum One Bit Operations to Make Integers Zero
Topic: Dynamic Programming, Bit Manipulation, Memoization
Difficulty: Hard
Problem:
Given an integer
n, you must transform it into 0 using the following operations any number of times:• Change the rightmost (
0^th) bit in the binary representation of n.• Change the
i^th bit in the binary representation of n if the (i-1)^th bit is set to 1 and the (i-2)^th through 0^th bits are set to 0.Return the minimum number of operations to transform
n into 0.Example 1:
Input: n = 3
Output: 2
Explanation: The binary representation of 3 is "11".
"11" -> "01" with the 2^nd operation since the 0^th bit is 1.
"01" -> "00" with the 1^st operation.
Example 2:
Input: n = 6
Output: 4
Explanation: The binary representation of 6 is "110".
"110" -> "010" with the 2^nd operation since the 1^st bit is 1 and 0^th through 0^th bits are 0.
"010" -> "011" with the 1^st operation.
"011" -> "001" with the 2^nd operation since the 0^th bit is 1.
"001" -> "000" with the 1^st operation.
Constraints:
•
0 <= n <= 10^92025-11-09
2169. Count Operations to Obtain Zero
Topic: Math, Simulation
Difficulty: Easy
Problem:
You are given two non-negative integers
In one operation, if
• For example, if
Return the number of operations required to make either
Example 1:
Example 2:
Constraints:
•
2169. Count Operations to Obtain Zero
Topic: Math, Simulation
Difficulty: Easy
Problem:
You are given two non-negative integers
num1 and num2.In one operation, if
num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2.• For example, if
num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1.Return the number of operations required to make either
num1 = 0 or num2 = 0.Example 1:
Input: num1 = 2, num2 = 3
Output: 3
Explanation:
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
So the total number of operations required is 3.
Example 2:
Input: num1 = 10, num2 = 10
Output: 1
Explanation:
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
So the total number of operations required is 1.
Constraints:
•
0 <= num1, num2 <= 10^52025-11-10
3542. Minimum Operations to Convert All Elements to Zero
Topic: Array, Hash Table, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
You are given an array
In one operation, you can select a subarray
Return the minimum number of operations required to make all elements in the array 0.
Example 1:
Input: nums = 0,2
Output: 1
Explanation:
• Select the subarray
• Thus, the minimum number of operations required is 1.
Example 2:
Input: nums = 3,1,2,1
Output: 3
Explanation:
• Select subarray
• Select subarray
• Select subarray
• Thus, the minimum number of operations required is 3.
Example 3:
Input: nums = 1,2,1,2,1,2
Output: 4
Explanation:
• Select subarray
• Select subarray
• Select subarray
• Select subarray
• Thus, the minimum number of operations required is 4.
Constraints:
•
•
3542. Minimum Operations to Convert All Elements to Zero
Topic: Array, Hash Table, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
You are given an array
nums of size n, consisting of non-negative integers. Your task is to apply some (possibly zero) operations on the array so that all elements become 0.In one operation, you can select a subarray
[i, j] (where 0 <= i <= j < n) and set all occurrences of the minimum non-negative integer in that subarray to 0.Return the minimum number of operations required to make all elements in the array 0.
Example 1:
Input: nums = 0,2
Output: 1
Explanation:
• Select the subarray
[1,1] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0].• Thus, the minimum number of operations required is 1.
Example 2:
Input: nums = 3,1,2,1
Output: 3
Explanation:
• Select subarray
[1,3] (which is [1,2,1]), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in [3,0,2,0].• Select subarray
[2,2] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [3,0,0,0].• Select subarray
[0,0] (which is [3]), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in [0,0,0,0].• Thus, the minimum number of operations required is 3.
Example 3:
Input: nums = 1,2,1,2,1,2
Output: 4
Explanation:
• Select subarray
[0,5] (which is [1,2,1,2,1,2]), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in [0,2,0,2,0,2].• Select subarray
[1,1] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,2,0,2].• Select subarray
[3,3] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,0,0,2].• Select subarray
[5,5] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,0,0,0].• Thus, the minimum number of operations required is 4.
Constraints:
•
1 <= n == nums.length <= 10^5•
0 <= nums[i] <= 10^52025-11-11
474. Ones and Zeroes
Topic: Array, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array of binary strings
Return the size of the largest subset of
A set
Example 1:
Example 2:
Constraints:
•
•
•
•
474. Ones and Zeroes
Topic: Array, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array of binary strings
strs and two integers m and n.Return the size of the largest subset of
strs such that there are at most m 0's and n 1's in the subset.A set
x is a subset of a set y if all elements of x are also elements of y.Example 1:
Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
Output: 4
Explanation: The largest subset with at most 5 0's and 3 1's is {"10", "0001", "1", "0"}, so the answer is 4.
Other valid but smaller subsets include {"0001", "1"} and {"10", "1", "0"}.
{"111001"} is an invalid subset because it contains 4 1's, greater than the maximum of 3.
Example 2:
Input: strs = ["10","0","1"], m = 1, n = 1
Output: 2
Explanation: The largest subset is {"0", "1"}, so the answer is 2.
Constraints:
•
1 <= strs.length <= 600•
1 <= strs[i].length <= 100•
strs[i] consists only of digits '0' and '1'.•
1 <= m, n <= 1002025-11-12
2654. Minimum Number of Operations to Make All Array Elements Equal to 1
Topic: Array, Math, Number Theory
Difficulty: Medium
Problem:
You are given a 0-indexed array
• Select an index
Return the minimum number of operations to make all elements of
The gcd of two integers is the greatest common divisor of the two integers.
Example 1:
Example 2:
Constraints:
•
•
2654. Minimum Number of Operations to Make All Array Elements Equal to 1
Topic: Array, Math, Number Theory
Difficulty: Medium
Problem:
You are given a 0-indexed array
nums consisiting of positive integers. You can do the following operation on the array any number of times:• Select an index
i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value.Return the minimum number of operations to make all elements of
nums equal to 1. If it is impossible, return -1.The gcd of two integers is the greatest common divisor of the two integers.
Example 1:
Input: nums = [2,6,3,4]
Output: 4
Explanation: We can do the following operations:
- Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4].
- Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4].
- Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4].
- Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1].
Example 2:
Input: nums = [2,10,6,14]
Output: -1
Explanation: It can be shown that it is impossible to make all the elements equal to 1.
Constraints:
•
2 <= nums.length <= 50•
1 <= nums[i] <= 10^62025-11-13
3228. Maximum Number of Operations to Move Ones to the End
Topic: String, Greedy, Counting
Difficulty: Medium
Problem:
You are given a binary string
You can perform the following operation on the string any number of times:
• Choose any index
• Move the character
Return the maximum number of operations that you can perform.
Example 1:
Input: s = "1001101"
Output: 4
Explanation:
We can perform the following operations:
• Choose index
• Choose index
• Choose index
• Choose index
Example 2:
Input: s = "00111"
Output: 0
Constraints:
•
•
3228. Maximum Number of Operations to Move Ones to the End
Topic: String, Greedy, Counting
Difficulty: Medium
Problem:
You are given a binary string
s.You can perform the following operation on the string any number of times:
• Choose any index
i from the string where i + 1 < s.length such that s[i] == '1' and s[i + 1] == '0'.• Move the character
s[i] to the right until it reaches the end of the string or another '1'. For example, for s = "010010", if we choose i = 1, the resulting string will be s = "000110".Return the maximum number of operations that you can perform.
Example 1:
Input: s = "1001101"
Output: 4
Explanation:
We can perform the following operations:
• Choose index
i = 0. The resulting string is s = "0011101".• Choose index
i = 4. The resulting string is s = "0011011".• Choose index
i = 3. The resulting string is s = "0010111".• Choose index
i = 2. The resulting string is s = "0001111".Example 2:
Input: s = "00111"
Output: 0
Constraints:
•
1 <= s.length <= 10^5•
s[i] is either '0' or '1'.2025-11-14
2536. Increment Submatrices by One
Topic: Array, Matrix, Prefix Sum
Difficulty: Medium
Problem:
You are given a positive integer
You are also given a 2D integer array
• Add
Return the matrix
Example 1:
Image: https://assets.leetcode.com/uploads/2022/11/24/p2example11.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/11/24/p2example22.png
Constraints:
•
•
•
•
2536. Increment Submatrices by One
Topic: Array, Matrix, Prefix Sum
Difficulty: Medium
Problem:
You are given a positive integer
n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes.You are also given a 2D integer array
query. For each query[i] = [row1_i, col1_i, row2_i, col2_i], you should do the following operation:• Add
1 to every element in the submatrix with the top left corner (row1_i, col1_i) and the bottom right corner (row2_i, col2_i). That is, add 1 to mat[x][y] for all row1_i <= x <= row2_i and col1_i <= y <= col2_i.Return the matrix
mat after performing every query.Example 1:
Image: https://assets.leetcode.com/uploads/2022/11/24/p2example11.png
Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]]
Output: [[1,1,0],[1,2,1],[0,1,1]]
Explanation: The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query.
- In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2).
- In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1).
Example 2:
Image: https://assets.leetcode.com/uploads/2022/11/24/p2example22.png
Input: n = 2, queries = [[0,0,1,1]]
Output: [[1,1],[1,1]]
Explanation: The diagram above shows the initial matrix and the matrix after the first query.
- In the first query we add 1 to every element in the matrix.
Constraints:
•
1 <= n <= 500•
1 <= queries.length <= 10^4•
0 <= row1_i <= row2_i < n•
0 <= col1_i <= col2_i < n2025-11-15
3234. Count the Number of Substrings With Dominant Ones
Topic: String, Sliding Window, Enumeration
Difficulty: Medium
Problem:
You are given a binary string
Return the number of substrings with dominant ones.
A string has dominant ones if the number of ones in the string is greater than or equal to the square of the number of zeros in the string.
Example 1:
Input: s = "00011"
Output: 5
Explanation:
The substrings with dominant ones are shown in the table below.
ijsi..jNumber of ZerosNumber of Ones33101441012301113411022401112
Example 2:
Input: s = "101101"
Output: 16
Explanation:
The substrings with non-dominant ones are shown in the table below.
Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
ijsi..jNumber of ZerosNumber of Ones110104401014011022041011023150110123
Constraints:
•
•
3234. Count the Number of Substrings With Dominant Ones
Topic: String, Sliding Window, Enumeration
Difficulty: Medium
Problem:
You are given a binary string
s.Return the number of substrings with dominant ones.
A string has dominant ones if the number of ones in the string is greater than or equal to the square of the number of zeros in the string.
Example 1:
Input: s = "00011"
Output: 5
Explanation:
The substrings with dominant ones are shown in the table below.
ijsi..jNumber of ZerosNumber of Ones33101441012301113411022401112
Example 2:
Input: s = "101101"
Output: 16
Explanation:
The substrings with non-dominant ones are shown in the table below.
Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
ijsi..jNumber of ZerosNumber of Ones110104401014011022041011023150110123
Constraints:
•
1 <= s.length <= 4 * 10^4•
s consists only of characters '0' and '1'.2025-11-16
1513. Number of Substrings With Only 1s
Topic: Math, String
Difficulty: Medium
Problem:
Given a binary string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1513. Number of Substrings With Only 1s
Topic: Math, String
Difficulty: Medium
Problem:
Given a binary string
s, return the number of substrings with all characters 1's. Since the answer may be too large, return it modulo 10^9 + 7.Example 1:
Input: s = "0110111"
Output: 9
Explanation: There are 9 substring in total with only 1's characters.
"1" -> 5 times.
"11" -> 3 times.
"111" -> 1 time.
Example 2:
Input: s = "101"
Output: 2
Explanation: Substring "1" is shown 2 times in s.
Example 3:
Input: s = "111111"
Output: 21
Explanation: Each substring contains only 1's characters.
Constraints:
•
1 <= s.length <= 10^5•
s[i] is either '0' or '1'.2025-11-17
1437. Check If All 1's Are at Least Length K Places Away
Topic: Array
Difficulty: Easy
Problem:
Given an binary array
Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png
Constraints:
•
•
•
1437. Check If All 1's Are at Least Length K Places Away
Topic: Array
Difficulty: Easy
Problem:
Given an binary array
nums and an integer k, return true if all 1's are at least k places away from each other, otherwise return false.Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Constraints:
•
1 <= nums.length <= 10^5•
0 <= k <= nums.length•
nums[i] is 0 or 12025-11-18
717. 1-bit and 2-bit Characters
Topic: Array
Difficulty: Easy
Problem:
We have two special characters:
• The first character can be represented by one bit
• The second character can be represented by two bits (
Given a binary array
Example 1:
Example 2:
Constraints:
•
•
717. 1-bit and 2-bit Characters
Topic: Array
Difficulty: Easy
Problem:
We have two special characters:
• The first character can be represented by one bit
0.• The second character can be represented by two bits (
10 or 11).Given a binary array
bits that ends with 0, return true if the last character must be a one-bit character.Example 1:
Input: bits = [1,0,0]
Output: true
Explanation: The only way to decode it is two-bit character and one-bit character.
So the last character is one-bit character.
Example 2:
Input: bits = [1,1,1,0]
Output: false
Explanation: The only way to decode it is two-bit character and two-bit character.
So the last character is not one-bit character.
Constraints:
•
1 <= bits.length <= 1000•
bits[i] is either 0 or 1.2025-11-19
2154. Keep Multiplying Found Values by Two
Topic: Array, Hash Table, Sorting, Simulation
Difficulty: Easy
Problem:
You are given an array of integers
You then do the following steps:
1. If
2. Otherwise, stop the process.
3. Repeat this process with the new number as long as you keep finding the number.
Return the final value of
Example 1:
Example 2:
Constraints:
•
•
2154. Keep Multiplying Found Values by Two
Topic: Array, Hash Table, Sorting, Simulation
Difficulty: Easy
Problem:
You are given an array of integers
nums. You are also given an integer original which is the first number that needs to be searched for in nums.You then do the following steps:
1. If
original is found in nums, multiply it by two (i.e., set original = 2 * original).2. Otherwise, stop the process.
3. Repeat this process with the new number as long as you keep finding the number.
Return the final value of
original.Example 1:
Input: nums = [5,3,6,1,12], original = 3
Output: 24
Explanation:
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
- 24 is not found in nums. Thus, 24 is returned.
Example 2:
Input: nums = [2,7,9], original = 4
Output: 4
Explanation:
- 4 is not found in nums. Thus, 4 is returned.
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i], original <= 1000