2025-10-27
2125. Number of Laser Beams in a Bank
Topic: Array, Math, String, Matrix
Difficulty: Medium
Problem:
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array
There is one laser beam between any two security devices if both conditions are met:
• The two devices are located on two different rows:
• For each row
Laser beams are independent, i.e., one beam does not interfere nor join with another.
Return the total number of laser beams in the bank.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg
Constraints:
•
•
•
•
2125. Number of Laser Beams in a Bank
Topic: Array, Math, String, Matrix
Difficulty: Medium
Problem:
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array
bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the i^th row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device.There is one laser beam between any two security devices if both conditions are met:
• The two devices are located on two different rows:
r_1 and r_2, where r_1 < r_2.• For each row
i where r_1 < i < r_2, there are no security devices in the i^th row.Laser beams are independent, i.e., one beam does not interfere nor join with another.
Return the total number of laser beams in the bank.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg
Input: bank = ["011001","000000","010100","001000"]
Output: 8
Explanation: Between each of the following device pairs, there is one beam. In total, there are 8 beams:
* bank[0][1] -- bank[2][1]
* bank[0][1] -- bank[2][3]
* bank[0][2] -- bank[2][1]
* bank[0][2] -- bank[2][3]
* bank[0][5] -- bank[2][1]
* bank[0][5] -- bank[2][3]
* bank[2][1] -- bank[3][2]
* bank[2][3] -- bank[3][2]
Note that there is no beam between any device on the 0^th row with any on the 3^rd row.
This is because the 2^nd row contains security devices, which breaks the second condition.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg
Input: bank = ["000","111","000"]
Output: 0
Explanation: There does not exist two devices located on two different rows.
Constraints:
•
m == bank.length•
n == bank[i].length•
1 <= m, n <= 500•
bank[i][j] is either '0' or '1'.2025-10-28
3354. Make Array Elements Equal to Zero
Topic: Array, Simulation, Prefix Sum
Difficulty: Easy
Problem:
You are given an integer array
Start by selecting a starting position
After that, you repeat the following process:
• If
• If
• Else if
• Decrement
• Reverse your movement direction (left becomes right and vice versa).
• Take a step in your new direction.
A selection of the initial position
Return the number of possible valid selections.
Example 1:
Input: nums = 1,0,2,0,3
Output: 2
Explanation:
The only possible valid selections are the following:
• Choose
•
• Choose
•
Example 2:
Input: nums = 2,3,4,0,4,1,0
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
•
•
• There is at least one element
3354. Make Array Elements Equal to Zero
Topic: Array, Simulation, Prefix Sum
Difficulty: Easy
Problem:
You are given an integer array
nums.Start by selecting a starting position
curr such that nums[curr] == 0, and choose a movement direction of either left or right.After that, you repeat the following process:
• If
curr is out of the range [0, n - 1], this process ends.• If
nums[curr] == 0, move in the current direction by incrementing curr if you are moving right, or decrementing curr if you are moving left.• Else if
nums[curr] > 0:• Decrement
nums[curr] by 1.• Reverse your movement direction (left becomes right and vice versa).
• Take a step in your new direction.
A selection of the initial position
curr and movement direction is considered valid if every element in nums becomes 0 by the end of the process.Return the number of possible valid selections.
Example 1:
Input: nums = 1,0,2,0,3
Output: 2
Explanation:
The only possible valid selections are the following:
• Choose
curr = 3, and a movement direction to the left.•
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0].• Choose
curr = 3, and a movement direction to the right.•
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].Example 2:
Input: nums = 2,3,4,0,4,1,0
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 100• There is at least one element
i where nums[i] == 0.2025-10-29
3370. Smallest Number With All Set Bits
Topic: Math, Bit Manipulation
Difficulty: Easy
Problem:
You are given a positive number
Return the smallest number
Example 1:
Input: n = 5
Output: 7
Explanation:
The binary representation of 7 is
Example 2:
Input: n = 10
Output: 15
Explanation:
The binary representation of 15 is
Example 3:
Input: n = 3
Output: 3
Explanation:
The binary representation of 3 is
Constraints:
•
3370. Smallest Number With All Set Bits
Topic: Math, Bit Manipulation
Difficulty: Easy
Problem:
You are given a positive number
n.Return the smallest number
x greater than or equal to n, such that the binary representation of x contains only set bitsExample 1:
Input: n = 5
Output: 7
Explanation:
The binary representation of 7 is
"111".Example 2:
Input: n = 10
Output: 15
Explanation:
The binary representation of 15 is
"1111".Example 3:
Input: n = 3
Output: 3
Explanation:
The binary representation of 3 is
"11".Constraints:
•
1 <= n <= 10002025-10-30
1526. Minimum Number of Increments on Subarrays to Form a Target Array
Topic: Array, Dynamic Programming, Stack, Greedy, Monotonic Stack
Difficulty: Hard
Problem:
You are given an integer array
In one operation you can choose any subarray from
Return the minimum number of operations to form a
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1526. Minimum Number of Increments on Subarrays to Form a Target Array
Topic: Array, Dynamic Programming, Stack, Greedy, Monotonic Stack
Difficulty: Hard
Problem:
You are given an integer array
target. You have an integer array initial of the same size as target with all elements initially zeros.In one operation you can choose any subarray from
initial and increment each value by one.Return the minimum number of operations to form a
target array from initial.The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2].
Constraints:
•
1 <= target.length <= 10^5•
1 <= target[i] <= 10^52025-10-31
3289. The Two Sneaky Numbers of Digitville
Topic: Array, Hash Table, Math
Difficulty: Easy
Problem:
In the town of Digitville, there was a list of numbers called
As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Example 1:
Input: nums = 0,1,1,0
Output: 0,1
Explanation:
The numbers 0 and 1 each appear twice in the array.
Example 2:
Input: nums = 0,3,2,1,3,2
Output: 2,3
Explanation:
The numbers 2 and 3 each appear twice in the array.
Example 3:
Input: nums = 7,1,5,4,3,4,6,0,9,5,8,2
Output: 4,5
Explanation:
The numbers 4 and 5 each appear twice in the array.
Constraints:
•
•
•
• The input is generated such that
3289. The Two Sneaky Numbers of Digitville
Topic: Array, Hash Table, Math
Difficulty: Easy
Problem:
In the town of Digitville, there was a list of numbers called
nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Example 1:
Input: nums = 0,1,1,0
Output: 0,1
Explanation:
The numbers 0 and 1 each appear twice in the array.
Example 2:
Input: nums = 0,3,2,1,3,2
Output: 2,3
Explanation:
The numbers 2 and 3 each appear twice in the array.
Example 3:
Input: nums = 7,1,5,4,3,4,6,0,9,5,8,2
Output: 4,5
Explanation:
The numbers 4 and 5 each appear twice in the array.
Constraints:
•
2 <= n <= 100•
nums.length == n + 2•
0 <= nums[i] < n• The input is generated such that
nums contains exactly two repeated elements.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'.