2024-10-25
1233. Remove Sub-Folders from the Filesystem
Topic: Array, String, Depth-First Search, Trie
Difficulty: Medium
Problem:
Given a list of folders
If a
The format of a path is one or more concatenated strings of the form:
• For example,
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
• Each folder name is unique.
1233. Remove Sub-Folders from the Filesystem
Topic: Array, String, Depth-First Search, Trie
Difficulty: Medium
Problem:
Given a list of folders
folder, return the folders after removing all sub-folders in those folders. You may return the answer in any order.If a
folder[i] is located within another folder[j], it is called a sub-folder of it. A sub-folder of folder[j] must start with folder[j], followed by a "/". For example, "/a/b" is a sub-folder of "/a", but "/b" is not a sub-folder of "/a/b/c".The format of a path is one or more concatenated strings of the form:
'/' followed by one or more lowercase English letters.• For example,
"/leetcode" and "/leetcode/problems" are valid paths while an empty string and "/" are not.Example 1:
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
Output: ["/a","/c/d","/c/f"]
Explanation: Folders "/a/b" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.
Example 2:
Input: folder = ["/a","/a/b/c","/a/b/d"]
Output: ["/a"]
Explanation: Folders "/a/b/c" and "/a/b/d" will be removed because they are subfolders of "/a".
Example 3:
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
Output: ["/a/b/c","/a/b/ca","/a/b/d"]
Constraints:
•
1 <= folder.length <= 4 * 10^4•
2 <= folder[i].length <= 100•
folder[i] contains only lowercase letters and '/'.•
folder[i] always starts with the character '/'.• Each folder name is unique.
2024-10-26
2458. Height of Binary Tree After Subtree Removal Queries
Topic: Array, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Hard
Problem:
You are given the
You have to perform
• Remove the subtree rooted at the node with the value
Return an array
Note:
• The queries are independent, so the tree returns to its initial state after each query.
• The height of a tree is the number of edges in the longest simple path from the root to some node in the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png
Constraints:
• The number of nodes in the tree is
•
•
• All the values in the tree are unique.
•
•
•
•
2458. Height of Binary Tree After Subtree Removal Queries
Topic: Array, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Hard
Problem:
You are given the
root of a binary tree with n nodes. Each node is assigned a unique value from 1 to n. You are also given an array queries of size m.You have to perform
m independent queries on the tree where in the i^th query you do the following:• Remove the subtree rooted at the node with the value
queries[i] from the tree. It is guaranteed that queries[i] will not be equal to the value of the root.Return an array
answer of size m where answer[i] is the height of the tree after performing the i^th query.Note:
• The queries are independent, so the tree returns to its initial state after each query.
• The height of a tree is the number of edges in the longest simple path from the root to some node in the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png
Input: root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
Output: [2]
Explanation: The diagram above shows the tree after removing the subtree rooted at node with value 4.
The height of the tree is 2 (The path 1 -> 3 -> 2).
Example 2:
Image: https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png
Input: root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
Output: [3,2,3,2]
Explanation: We have the following queries:
- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).
Constraints:
• The number of nodes in the tree is
n.•
2 <= n <= 10^5•
1 <= Node.val <= n• All the values in the tree are unique.
•
m == queries.length•
1 <= m <= min(n, 10^4)•
1 <= queries[i] <= n•
queries[i] != root.val2024-10-27
1277. Count Square Submatrices with All Ones
Topic: Array, Dynamic Programming, Matrix
Difficulty: Medium
Problem:
Given a
Example 1:
Example 2:
Constraints:
•
•
•
1277. Count Square Submatrices with All Ones
Topic: Array, Dynamic Programming, Matrix
Difficulty: Medium
Problem:
Given a
m * n matrix of ones and zeros, return how many square submatrices have all ones.Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
•
1 <= arr.length <= 300•
1 <= arr[0].length <= 300•
0 <= arr[i][j] <= 12024-10-28
2501. Longest Square Streak in an Array
Topic: Array, Hash Table, Binary Search, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
You are given an integer array
• The length of the subsequence is at least
• after sorting the subsequence, each element (except the first element) is the square of the previous number.
Return the length of the longest square streak in
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Example 2:
Constraints:
•
•
2501. Longest Square Streak in an Array
Topic: Array, Hash Table, Binary Search, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
You are given an integer array
nums. A subsequence of nums is called a square streak if:• The length of the subsequence is at least
2, and• after sorting the subsequence, each element (except the first element) is the square of the previous number.
Return the length of the longest square streak in
nums, or return -1 if there is no square streak.A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [4,3,6,16,8,2]
Output: 3
Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].
- 4 = 2 * 2.
- 16 = 4 * 4.
Therefore, [4,16,2] is a square streak.
It can be shown that every subsequence of length 4 is not a square streak.
Example 2:
Input: nums = [2,3,5,6,7]
Output: -1
Explanation: There is no square streak in nums so return -1.
Constraints:
•
2 <= nums.length <= 10^5•
2 <= nums[i] <= 10^52024-10-29
2684. Maximum Number of Moves in a Grid
Topic: Array, Dynamic Programming, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed
You can start at any cell in the first column of the matrix, and traverse the grid in the following way:
• From a cell
Return the maximum number of moves that you can perform.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/04/11/yetgriddrawio-10.png
Example 2:
Constraints:
•
•
•
•
•
2684. Maximum Number of Moves in a Grid
Topic: Array, Dynamic Programming, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed
m x n matrix grid consisting of positive integers.You can start at any cell in the first column of the matrix, and traverse the grid in the following way:
• From a cell
(row, col), you can move to any of the cells: (row - 1, col + 1), (row, col + 1) and (row + 1, col + 1) such that the value of the cell you move to, should be strictly bigger than the value of the current cell.Return the maximum number of moves that you can perform.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/04/11/yetgriddrawio-10.png
Input: grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]
Output: 3
Explanation: We can start at the cell (0, 0) and make the following moves:
- (0, 0) -> (0, 1).
- (0, 1) -> (1, 2).
- (1, 2) -> (2, 3).
It can be shown that it is the maximum number of moves that can be made.
Example 2:
Image: [https://assets.leetcode.com/uploads/2023/04/12/yetgrid4drawio.png](https://assets.leetcode.com/uploads/2023/04/12/yetgrid4drawio.png)
Input: grid = [[3,2,4],[2,1,9],[1,1,7]]
Output: 0
Explanation: Starting from any cell in the first column we cannot perform any moves.
Constraints:
•
m == grid.length•
n == grid[i].length•
2 <= m, n <= 1000•
4 <= m * n <= 10^5•
1 <= grid[i][j] <= 10^62024-10-30
1671. Minimum Number of Removals to Make Mountain Array
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
You may recall that an array
•
• There exists some index
•
•
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
• It is guaranteed that you can make a mountain array out of
1671. Minimum Number of Removals to Make Mountain Array
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
You may recall that an array
arr is a mountain array if and only if:•
arr.length >= 3• There exists some index
i (0-indexed) with 0 < i < arr.length - 1 such that:•
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]•
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]Given an integer array
nums, return the minimum number of elements to remove to make nums a mountain array.Example 1:
Input: nums = [1,3,1]
Output: 0
Explanation: The array itself is a mountain array so we do not need to remove any elements.
Example 2:
Input: nums = [2,1,1,5,6,2,3,1]
Output: 3
Explanation: One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].
Constraints:
•
3 <= nums.length <= 1000•
1 <= nums[i] <= 10^9• It is guaranteed that you can make a mountain array out of
nums.2024-10-31
2463. Minimum Total Distance Traveled
Topic: Array, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
There are some robots and factories on the X-axis. You are given an integer array
The positions of each robot are unique. The positions of each factory are also unique. Note that a robot can be in the same position as a factory initially.
All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.
At any moment, you can set the initial direction of moving for some robot. Your target is to minimize the total distance traveled by all the robots.
Return the minimum total distance traveled by all the robots. The test cases are generated such that all the robots can be repaired.
Note that
• All robots move at the same speed.
• If two robots move in the same direction, they will never collide.
• If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.
• If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.
• If the robot moved from a position
Example 1:
Image: https://assets.leetcode.com/uploads/2022/09/15/example1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2022/09/15/example-2.jpg
Constraints:
•
•
•
•
• The input will be generated such that it is always possible to repair every robot.
2463. Minimum Total Distance Traveled
Topic: Array, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
There are some robots and factories on the X-axis. You are given an integer array
robot where robot[i] is the position of the i^th robot. You are also given a 2D integer array factory where factory[j] = [position_j, limit_j] indicates that position_j is the position of the j^th factory and that the j^th factory can repair at most limit_j robots.The positions of each robot are unique. The positions of each factory are also unique. Note that a robot can be in the same position as a factory initially.
All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.
At any moment, you can set the initial direction of moving for some robot. Your target is to minimize the total distance traveled by all the robots.
Return the minimum total distance traveled by all the robots. The test cases are generated such that all the robots can be repaired.
Note that
• All robots move at the same speed.
• If two robots move in the same direction, they will never collide.
• If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.
• If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.
• If the robot moved from a position
x to a position y, the distance it moved is |y - x|.Example 1:
Image: https://assets.leetcode.com/uploads/2022/09/15/example1.jpg
Input: robot = [0,4,6], factory = [[2,2],[6,2]]
Output: 4
Explanation: As shown in the figure:
- The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.
- The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.
- The third robot at position 6 will be repaired at the second factory. It does not need to move.
The limit of the first factory is 2, and it fixed 2 robots.
The limit of the second factory is 2, and it fixed 1 robot.
The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/09/15/example-2.jpg
Input: robot = [1,-1], factory = [[-2,1],[2,1]]
Output: 2
Explanation: As shown in the figure:
- The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.
- The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.
The limit of the first factory is 1, and it fixed 1 robot.
The limit of the second factory is 1, and it fixed 1 robot.
The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.
Constraints:
•
1 <= robot.length, factory.length <= 100•
factory[j].length == 2•
-10^9 <= robot[i], position_j <= 10^9•
0 <= limit_j <= robot.length• The input will be generated such that it is always possible to repair every robot.
2024-11-01
1957. Delete Characters to Make Fancy String
Topic: String
Difficulty: Easy
Problem:
A fancy string is a string where no three consecutive characters are equal.
Given a string
Return the final string after the deletion. It can be shown that the answer will always be unique.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1957. Delete Characters to Make Fancy String
Topic: String
Difficulty: Easy
Problem:
A fancy string is a string where no three consecutive characters are equal.
Given a string
s, delete the minimum possible number of characters from s to make it fancy.Return the final string after the deletion. It can be shown that the answer will always be unique.
Example 1:
Input: s = "leeetcode"
Output: "leetcode"
Explanation:
Remove an 'e' from the first group of 'e's to create "leetcode".
No three consecutive characters are equal, so return "leetcode".
Example 2:
Input: s = "aaabaaaa"
Output: "aabaa"
Explanation:
Remove an 'a' from the first group of 'a's to create "aabaaaa".
Remove two 'a's from the second group of 'a's to create "aabaa".
No three consecutive characters are equal, so return "aabaa".
Example 3:
Input: s = "aab"
Output: "aab"
Explanation: No three consecutive characters are equal, so return "aab".
Constraints:
•
1 <= s.length <= 10^5•
s consists only of lowercase English letters.2024-11-02
2490. Circular Sentence
Topic: String
Difficulty: Easy
Problem:
A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
• For example,
Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.
A sentence is circular if:
• The last character of a word is equal to the first character of the next word.
• The last character of the last word is equal to the first character of the first word.
For example,
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• The words in
• There are no leading or trailing spaces.
2490. Circular Sentence
Topic: String
Difficulty: Easy
Problem:
A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
• For example,
"Hello World", "HELLO", "hello world hello world" are all sentences.Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.
A sentence is circular if:
• The last character of a word is equal to the first character of the next word.
• The last character of the last word is equal to the first character of the first word.
For example,
"leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not circular sentences.Given a string
sentence, return true if it is circular. Otherwise, return false.Example 1:
Input: sentence = "leetcode exercises sound delightful"
Output: true
Explanation: The words in sentence are ["leetcode", "exercises", "sound", "delightful"].
- leetcode's last character is equal to exercises's first character.
- exercises's last character is equal to sound's first character.
- sound's last character is equal to delightful's first character.
- delightful's last character is equal to leetcode's first character.
The sentence is circular.
Example 2:
Input: sentence = "eetcode"
Output: true
Explanation: The words in sentence are ["eetcode"].
- eetcode's last character is equal to eetcode's first character.
The sentence is circular.
Example 3:
Input: sentence = "Leetcode is cool"
Output: false
Explanation: The words in sentence are ["Leetcode", "is", "cool"].
- Leetcode's last character is not equal to is's first character.
The sentence is not circular.
Constraints:
•
1 <= sentence.length <= 500•
sentence consist of only lowercase and uppercase English letters and spaces.• The words in
sentence are separated by a single space.• There are no leading or trailing spaces.
2024-11-03
796. Rotate String
Topic: String, String Matching
Difficulty: Easy
Problem:
Given two strings
A shift on
• For example, if
Example 1:
Example 2:
Constraints:
•
•
796. Rotate String
Topic: String, String Matching
Difficulty: Easy
Problem:
Given two strings
s and goal, return true if and only if s can become goal after some number of shifts on s.A shift on
s consists of moving the leftmost character of s to the rightmost position.• For example, if
s = "abcde", then it will be "bcdea" after one shift.Example 1:
Input: s = "abcde", goal = "cdeab"
Output: true
Example 2:
Input: s = "abcde", goal = "abced"
Output: false
Constraints:
•
1 <= s.length, goal.length <= 100•
s and goal consist of lowercase English letters.2024-11-04
3163. String Compression III
Topic: String
Difficulty: Medium
Problem:
Given a string
• Begin with an empty string
• Remove a maximum length prefix of
• Append the length of the prefix followed by
Return the string
Example 1:
Input: word = "abcde"
Output: "1a1b1c1d1e"
Explanation:
Initially,
For each prefix, append
Example 2:
Input: word = "aaaaaaaaaaaaaabb"
Output: "9a5a2b"
Explanation:
Initially,
• For prefix
• For prefix
• For prefix
Constraints:
•
•
3163. String Compression III
Topic: String
Difficulty: Medium
Problem:
Given a string
word, compress it using the following algorithm:• Begin with an empty string
comp. While word is not empty, use the following operation:• Remove a maximum length prefix of
word made of a single character c repeating at most 9 times.• Append the length of the prefix followed by
c to comp.Return the string
comp.Example 1:
Input: word = "abcde"
Output: "1a1b1c1d1e"
Explanation:
Initially,
comp = "". Apply the operation 5 times, choosing "a", "b", "c", "d", and "e" as the prefix in each operation.For each prefix, append
"1" followed by the character to comp.Example 2:
Input: word = "aaaaaaaaaaaaaabb"
Output: "9a5a2b"
Explanation:
Initially,
comp = "". Apply the operation 3 times, choosing "aaaaaaaaa", "aaaaa", and "bb" as the prefix in each operation.• For prefix
"aaaaaaaaa", append "9" followed by "a" to comp.• For prefix
"aaaaa", append "5" followed by "a" to comp.• For prefix
"bb", append "2" followed by "b" to comp.Constraints:
•
1 <= word.length <= 2 * 10^5•
word consists only of lowercase English letters.2024-11-05
2914. Minimum Number of Changes to Make Binary String Beautiful
Topic: String
Difficulty: Medium
Problem:
You are given a 0-indexed binary string
A string is beautiful if it's possible to partition it into one or more substrings such that:
• Each substring has an even length.
• Each substring contains only
You can change any character in
Return the minimum number of changes required to make the string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2914. Minimum Number of Changes to Make Binary String Beautiful
Topic: String
Difficulty: Medium
Problem:
You are given a 0-indexed binary string
s having an even length.A string is beautiful if it's possible to partition it into one or more substrings such that:
• Each substring has an even length.
• Each substring contains only
1's or only 0's.You can change any character in
s to 0 or 1.Return the minimum number of changes required to make the string
s beautiful.Example 1:
Input: s = "1001"
Output: 2
Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100".
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
Example 2:
Input: s = "10"
Output: 1
Explanation: We change s[1] to 1 to get string "11".
It can be seen that the string "11" is beautiful because we can partition it into "11".
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
Example 3:
Input: s = "0000"
Output: 0
Explanation: We don't need to make any changes as the string "0000" is beautiful already.
Constraints:
•
2 <= s.length <= 10^5•
s has an even length.•
s[i] is either '0' or '1'.2024-11-06
3011. Find if Array Can Be Sorted
Topic: Array, Bit Manipulation, Sorting
Difficulty: Medium
Problem:
You are given a 0-indexed array of positive integers
In one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times (including zero).
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
3011. Find if Array Can Be Sorted
Topic: Array, Bit Manipulation, Sorting
Difficulty: Medium
Problem:
You are given a 0-indexed array of positive integers
nums.In one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times (including zero).
Return
true if you can sort the array, else return false.Example 1:
Input: nums = [8,4,2,30,15]
Output: true
Explanation: Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
We can sort the array using 4 operations:
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
The array has become sorted, hence we return true.
Note that there may be other sequences of operations which also sort the array.
Example 2:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: The array is already sorted, hence we return true.
Example 3:
Input: nums = [3,16,8,4,2]
Output: false
Explanation: It can be shown that it is not possible to sort the input array using any number of operations.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 2^82024-11-07
2275. Largest Combination With Bitwise AND Greater Than Zero
Topic: Array, Hash Table, Bit Manipulation, Counting
Difficulty: Medium
Problem:
The bitwise AND of an array
• For example, for
• Also, for
You are given an array of positive integers
Return the size of the largest combination of
Example 1:
Example 2:
Constraints:
•
•
2275. Largest Combination With Bitwise AND Greater Than Zero
Topic: Array, Hash Table, Bit Manipulation, Counting
Difficulty: Medium
Problem:
The bitwise AND of an array
nums is the bitwise AND of all integers in nums.• For example, for
nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.• Also, for
nums = [7], the bitwise AND is 7.You are given an array of positive integers
candidates. Evaluate the bitwise AND of every combination of numbers of candidates. Each number in candidates may only be used once in each combination.Return the size of the largest combination of
candidates with a bitwise AND greater than 0.Example 1:
Input: candidates = [16,17,71,62,12,24,14]
Output: 4
Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
The size of the combination is 4.
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
Note that more than one combination may have the largest size.
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
Example 2:
Input: candidates = [8,8]
Output: 2
Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
The size of the combination is 2, so we return 2.
Constraints:
•
1 <= candidates.length <= 10^5•
1 <= candidates[i] <= 10^72024-11-08
1829. Maximum XOR for Each Query
Topic: Array, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
You are given a sorted array
1. Find a non-negative integer
2. Remove the last element from the current array
Return an array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
1829. Maximum XOR for Each Query
Topic: Array, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
You are given a sorted array
nums of n non-negative integers and an integer maximumBit. You want to perform the following query n times:1. Find a non-negative integer
k < 2^maximumBit such that nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k is maximized. k is the answer to the i^th query.2. Remove the last element from the current array
nums.Return an array
answer, where answer[i] is the answer to the i^th query.Example 1:
Input: nums = [0,1,1,3], maximumBit = 2
Output: [0,3,2,3]
Explanation: The queries are answered as follows:
1^st query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.
2^nd query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.
3^rd query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.
4^th query: nums = [0], k = 3 since 0 XOR 3 = 3.
Example 2:
Input: nums = [2,3,4,7], maximumBit = 3
Output: [5,2,6,5]
Explanation: The queries are answered as follows:
1^st query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.
2^nd query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.
3^rd query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.
4^th query: nums = [2], k = 5 since 2 XOR 5 = 7.
Example 3:
Input: nums = [0,1,2,2,5,7], maximumBit = 3
Output: [4,3,6,4,6,7]
Constraints:
•
nums.length == n•
1 <= n <= 10^5•
1 <= maximumBit <= 20•
0 <= nums[i] < 2^maximumBit•
nums is sorted in ascending order.2024-11-09
3133. Minimum Array End
Topic: Bit Manipulation
Difficulty: Medium
Problem:
You are given two integers
Return the minimum possible value of
Example 1:
Input: n = 3, x = 4
Output: 6
Explanation:
Example 2:
Input: n = 2, x = 7
Output: 15
Explanation:
Constraints:
•
3133. Minimum Array End
Topic: Bit Manipulation
Difficulty: Medium
Problem:
You are given two integers
n and x. You have to construct an array of positive integers nums of size n where for every 0 <= i < n - 1, nums[i + 1] is greater than nums[i], and the result of the bitwise AND operation between all elements of nums is x.Return the minimum possible value of
nums[n - 1].Example 1:
Input: n = 3, x = 4
Output: 6
Explanation:
nums can be [4,5,6] and its last element is 6.Example 2:
Input: n = 2, x = 7
Output: 15
Explanation:
nums can be [7,15] and its last element is 15.Constraints:
•
1 <= n, x <= 10^82024-11-10
3097. Shortest Subarray With OR at Least K II
Topic: Array, Bit Manipulation, Sliding Window
Difficulty: Medium
Problem:
You are given an array
An array is called special if the bitwise
Return the length of the shortest special non-empty subarray of
Example 1:
Input: nums = 1,2,3, k = 2
Output: 1
Explanation:
The subarray
Example 2:
Input: nums = 2,1,8, k = 10
Output: 3
Explanation:
The subarray
Example 3:
Input: nums = 1,2, k = 0
Output: 1
Explanation:
The subarray
Constraints:
•
•
•
3097. Shortest Subarray With OR at Least K II
Topic: Array, Bit Manipulation, Sliding Window
Difficulty: Medium
Problem:
You are given an array
nums of non-negative integers and an integer k.An array is called special if the bitwise
OR of all of its elements is at least k.Return the length of the shortest special non-empty subarray of
nums, or return -1 if no special subarray exists.Example 1:
Input: nums = 1,2,3, k = 2
Output: 1
Explanation:
The subarray
[3] has OR value of 3. Hence, we return 1.Example 2:
Input: nums = 2,1,8, k = 10
Output: 3
Explanation:
The subarray
[2,1,8] has OR value of 11. Hence, we return 3.Example 3:
Input: nums = 1,2, k = 0
Output: 1
Explanation:
The subarray
[1] has OR value of 1. Hence, we return 1.Constraints:
•
1 <= nums.length <= 2 * 10^5•
0 <= nums[i] <= 10^9•
0 <= k <= 10^92024-11-11
2601. Prime Subtraction Operation
Topic: Array, Math, Binary Search, Greedy, Number Theory
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
You can perform the following operation as many times as you want:
• Pick an index
Return true if you can make
A strictly increasing array is an array whose each element is strictly greater than its preceding element.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2601. Prime Subtraction Operation
Topic: Array, Math, Binary Search, Greedy, Number Theory
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums of length n.You can perform the following operation as many times as you want:
• Pick an index
i that you haven’t picked before, and pick a prime p strictly less than nums[i], then subtract p from nums[i].Return true if you can make
nums a strictly increasing array using the above operation and false otherwise.A strictly increasing array is an array whose each element is strictly greater than its preceding element.
Example 1:
Input: nums = [4,9,6,10]
Output: true
Explanation: In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10].
In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10].
After the second operation, nums is sorted in strictly increasing order, so the answer is true.
Example 2:
Input: nums = [6,8,11,12]
Output: true
Explanation: Initially nums is sorted in strictly increasing order, so we don't need to make any operations.
Example 3:
Input: nums = [5,8,3]
Output: false
Explanation: It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= 1000•
nums.length == n2024-11-12
2070. Most Beautiful Item for Each Query
Topic: Array, Binary Search, Sorting
Difficulty: Medium
Problem:
You are given a 2D integer array
You are also given a 0-indexed integer array
Return an array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2070. Most Beautiful Item for Each Query
Topic: Array, Binary Search, Sorting
Difficulty: Medium
Problem:
You are given a 2D integer array
items where items[i] = [price_i, beauty_i] denotes the price and beauty of an item respectively.You are also given a 0-indexed integer array
queries. For each queries[j], you want to determine the maximum beauty of an item whose price is less than or equal to queries[j]. If no such item exists, then the answer to this query is 0.Return an array
answer of the same length as queries where answer[j] is the answer to the j^th query.Example 1:
Input: items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
Output: [2,4,5,5,6,6]
Explanation:
- For queries[0]=1, [1,2] is the only item which has price <= 1. Hence, the answer for this query is 2.
- For queries[1]=2, the items which can be considered are [1,2] and [2,4].
The maximum beauty among them is 4.
- For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5].
The maximum beauty among them is 5.
- For queries[4]=5 and queries[5]=6, all items can be considered.
Hence, the answer for them is the maximum beauty of all items, i.e., 6.
Example 2:
Input: items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
Output: [4]
Explanation:
The price of every item is equal to 1, so we choose the item with the maximum beauty 4.
Note that multiple items can have the same price and/or beauty.
Example 3:
Input: items = [[10,1000]], queries = [5]
Output: [0]
Explanation:
No item has a price less than or equal to 5, so no item can be chosen.
Hence, the answer to the query is 0.
Constraints:
•
1 <= items.length, queries.length <= 10^5•
items[i].length == 2•
1 <= price_i, beauty_i, queries[j] <= 10^92024-11-13
2563. Count the Number of Fair Pairs
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
Given a 0-indexed integer array
A pair
•
•
Example 1:
Example 2:
Constraints:
•
•
•
•
2563. Count the Number of Fair Pairs
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
Given a 0-indexed integer array
nums of size n and two integers lower and upper, return the number of fair pairs.A pair
(i, j) is fair if:•
0 <= i < j < n, and•
lower <= nums[i] + nums[j] <= upperExample 1:
Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6
Output: 6
Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).
Example 2:
Input: nums = [1,7,9,2,5], lower = 11, upper = 11
Output: 1
Explanation: There is a single fair pair: (2,3).
Constraints:
•
1 <= nums.length <= 10^5•
nums.length == n•
-10^9 <= nums[i] <= 10^9•
-10^9 <= lower <= upper <= 10^9