2025-07-16
3201. Find the Maximum Length of Valid Subsequence I
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an integer array
A subsequence
•
Return the length of the longest valid subsequence of
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 = 1,2,3,4
Output: 4
Explanation:
The longest valid subsequence is
Example 2:
Input: nums = 1,2,1,1,2,1,2
Output: 6
Explanation:
The longest valid subsequence is
Example 3:
Input: nums = 1,3
Output: 2
Explanation:
The longest valid subsequence is
Constraints:
•
•
3201. Find the Maximum Length of Valid Subsequence I
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an integer array
nums.A subsequence
sub of nums with length x is called valid if it satisfies:•
(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2.Return the length of the longest valid subsequence of
nums.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 = 1,2,3,4
Output: 4
Explanation:
The longest valid subsequence is
[1, 2, 3, 4].Example 2:
Input: nums = 1,2,1,1,2,1,2
Output: 6
Explanation:
The longest valid subsequence is
[1, 2, 1, 2, 1, 2].Example 3:
Input: nums = 1,3
Output: 2
Explanation:
The longest valid subsequence is
[1, 3].Constraints:
•
2 <= nums.length <= 2 * 10^5•
1 <= nums[i] <= 10^72025-07-17
3202. Find the Maximum Length of Valid Subsequence II
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an integer array
A subsequence
•
Return the length of the longest valid subsequence of
Example 1:
Input: nums = 1,2,3,4,5, k = 2
Output: 5
Explanation:
The longest valid subsequence is
Example 2:
Input: nums = 1,4,2,3,1,4, k = 3
Output: 4
Explanation:
The longest valid subsequence is
Constraints:
•
•
•
3202. Find the Maximum Length of Valid Subsequence II
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an integer array
nums and a positive integer k.A subsequence
sub of nums with length x is called valid if it satisfies:•
(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.Return the length of the longest valid subsequence of
nums.Example 1:
Input: nums = 1,2,3,4,5, k = 2
Output: 5
Explanation:
The longest valid subsequence is
[1, 2, 3, 4, 5].Example 2:
Input: nums = 1,4,2,3,1,4, k = 3
Output: 4
Explanation:
The longest valid subsequence is
[1, 4, 1, 4].Constraints:
•
2 <= nums.length <= 10^3•
1 <= nums[i] <= 10^7•
1 <= k <= 10^32025-07-18
2163. Minimum Difference in Sums After Removal of Elements
Topic: Array, Dynamic Programming, Heap (Priority Queue)
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
You are allowed to remove any subsequence of elements of size exactly
• The first
• The next
The difference in sums of the two parts is denoted as
• For example, if
• Similarly, if
Return the minimum difference possible between the sums of the two parts after the removal of
Example 1:
Example 2:
Constraints:
•
•
•
2163. Minimum Difference in Sums After Removal of Elements
Topic: Array, Dynamic Programming, Heap (Priority Queue)
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
nums consisting of 3 * n elements.You are allowed to remove any subsequence of elements of size exactly
n from nums. The remaining 2 * n elements will be divided into two equal parts:• The first
n elements belonging to the first part and their sum is sum_first.• The next
n elements belonging to the second part and their sum is sum_second.The difference in sums of the two parts is denoted as
sum_first - sum_second.• For example, if
sum_first = 3 and sum_second = 2, their difference is 1.• Similarly, if
sum_first = 2 and sum_second = 3, their difference is -1.Return the minimum difference possible between the sums of the two parts after the removal of
n elements.Example 1:
Input: nums = [3,1,2]
Output: -1
Explanation: Here, nums has 3 elements, so n = 1.
Thus we have to remove 1 element from nums and divide the array into two equal parts.
- If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two parts will be 1 - 2 = -1.
- If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two parts will be 3 - 2 = 1.
- If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two parts will be 3 - 1 = 2.
The minimum difference between sums of the two parts is min(-1,1,2) = -1.
Example 2:
Input: nums = [7,9,5,8,1,3]
Output: 1
Explanation: Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The difference in sums will be (7+9) - (1+3) = 12.
To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) - (8+3) = 1.
It can be shown that it is not possible to obtain a difference smaller than 1.
Constraints:
•
nums.length == 3 * n•
1 <= n <= 10^5•
1 <= nums[i] <= 10^52025-07-19
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.
2025-07-20
1948. Delete Duplicate Folders in System
Topic: Array, Hash Table, String, Trie, Hash Function
Difficulty: Hard
Problem:
Due to a bug, there are many duplicate folders in a file system. You are given a 2D array
• For example,
Two folders (not necessarily on the same level) are identical if they contain the same non-empty set of identical subfolders and underlying subfolder structure. The folders do not need to be at the root level to be identical. If two or more folders are identical, then mark the folders as well as all their subfolders.
• For example, folders
•
•
•
•
•
•
•
•
• However, if the file structure also included the path
Once all the identical folders and their subfolders have been marked, the file system will delete all of them. The file system only runs the deletion once, so any folders that become identical after the initial deletion are not deleted.
Return the 2D array
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder2.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder3.jpg
Constraints:
•
•
•
•
•
• No two paths lead to the same folder.
• For any folder not at the root level, its parent folder will also be in the input.
1948. Delete Duplicate Folders in System
Topic: Array, Hash Table, String, Trie, Hash Function
Difficulty: Hard
Problem:
Due to a bug, there are many duplicate folders in a file system. You are given a 2D array
paths, where paths[i] is an array representing an absolute path to the i^th folder in the file system.• For example,
["one", "two", "three"] represents the path "/one/two/three".Two folders (not necessarily on the same level) are identical if they contain the same non-empty set of identical subfolders and underlying subfolder structure. The folders do not need to be at the root level to be identical. If two or more folders are identical, then mark the folders as well as all their subfolders.
• For example, folders
"/a" and "/b" in the file structure below are identical. They (as well as their subfolders) should all be marked:•
/a•
/a/x•
/a/x/y•
/a/z•
/b•
/b/x•
/b/x/y•
/b/z• However, if the file structure also included the path
"/b/w", then the folders "/a" and "/b" would not be identical. Note that "/a/x" and "/b/x" would still be considered identical even with the added folder.Once all the identical folders and their subfolders have been marked, the file system will delete all of them. The file system only runs the deletion once, so any folders that become identical after the initial deletion are not deleted.
Return the 2D array
ans containing the paths of the remaining folders after deleting all the marked folders. The paths may be returned in any order.Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder1.jpg
Input: paths = [["a"],["c"],["d"],["a","b"],["c","b"],["d","a"]]
Output: [["d"],["d","a"]]
Explanation: The file structure is as shown.
Folders "/a" and "/c" (and their subfolders) are marked for deletion because they both contain an empty
folder named "b".
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder2.jpg
Input: paths = [["a"],["c"],["a","b"],["c","b"],["a","b","x"],["a","b","x","y"],["w"],["w","y"]]
Output: [["c"],["c","b"],["a"],["a","b"]]
Explanation: The file structure is as shown.
Folders "/a/b/x" and "/w" (and their subfolders) are marked for deletion because they both contain an empty folder named "y".
Note that folders "/a" and "/c" are identical after the deletion, but they are not deleted because they were not marked beforehand.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder3.jpg
Input: paths = [["a","b"],["c","d"],["c"],["a"]]
Output: [["c"],["c","d"],["a"],["a","b"]]
Explanation: All folders are unique in the file system.
Note that the returned array can be in a different order as the order does not matter.
Constraints:
•
1 <= paths.length <= 2 * 10^4•
1 <= paths[i].length <= 500•
1 <= paths[i][j].length <= 10•
1 <= sum(paths[i][j].length) <= 2 * 10^5•
path[i][j] consists of lowercase English letters.• No two paths lead to the same folder.
• For any folder not at the root level, its parent folder will also be in the input.
2025-07-21
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.2025-07-22
1695. Maximum Erasure Value
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are given an array of positive integers
Return the maximum score you can get by erasing exactly one subarray.
An array
Example 1:
Example 2:
Constraints:
•
•
1695. Maximum Erasure Value
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are given an array of positive integers
nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.Return the maximum score you can get by erasing exactly one subarray.
An array
b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).Example 1:
Input: nums = [4,2,4,5,6]
Output: 17
Explanation: The optimal subarray here is [2,4,5,6].
Example 2:
Input: nums = [5,2,1,2,5,2,1,2,5]
Output: 8
Explanation: The optimal subarray here is [5,2,1] or [1,2,5].
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^42025-07-23
1717. Maximum Score From Removing Substrings
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a string
• Remove substring
• For example, when removing
• Remove substring
• For example, when removing
Return the maximum points you can gain after applying the above operations on
Example 1:
Example 2:
Constraints:
•
•
•
1717. Maximum Score From Removing Substrings
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a string
s and two integers x and y. You can perform two types of operations any number of times.• Remove substring
"ab" and gain x points.• For example, when removing
"ab" from "cabxbae" it becomes "cxbae".• Remove substring
"ba" and gain y points.• For example, when removing
"ba" from "cabxbae" it becomes "cabxe".Return the maximum points you can gain after applying the above operations on
s.Example 1:
Input: s = "cdbcbbaaabab", x = 4, y = 5
Output: 19
Explanation:
- Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.
Example 2:
Input: s = "aabbaaxybbaabb", x = 5, y = 4
Output: 20
Constraints:
•
1 <= s.length <= 10^5•
1 <= x, y <= 10^4•
s consists of lowercase English letters.2025-07-24
2322. Minimum Score After Removals on a Tree
Topic: Array, Bit Manipulation, Tree, Depth-First Search
Difficulty: Hard
Problem:
There is an undirected connected tree with
You are given a 0-indexed integer array
Remove two distinct edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:
1. Get the XOR of all the values of the nodes for each of the three components respectively.
2. The difference between the largest XOR value and the smallest XOR value is the score of the pair.
• For example, say the three components have the node values:
Return the minimum score of any possible pair of edge removals on the given tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png
Constraints:
•
•
•
•
•
•
•
•
2322. Minimum Score After Removals on a Tree
Topic: Array, Bit Manipulation, Tree, Depth-First Search
Difficulty: Hard
Problem:
There is an undirected connected tree with
n nodes labeled from 0 to n - 1 and n - 1 edges.You are given a 0-indexed integer array
nums of length n where nums[i] represents the value of the i^th node. You are also given a 2D integer array edges of length n - 1 where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.Remove two distinct edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:
1. Get the XOR of all the values of the nodes for each of the three components respectively.
2. The difference between the largest XOR value and the smallest XOR value is the score of the pair.
• For example, say the three components have the node values:
[4,5,7], [1,9], and [3,3,3]. The three XOR values are 4 ^ 5 ^ 7 = 6, 1 ^ 9 = 8, and 3 ^ 3 ^ 3 = 3. The largest XOR value is 8 and the smallest XOR value is 3. The score is then 8 - 3 = 5.Return the minimum score of any possible pair of edge removals on the given tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png
Input: nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]
Output: 9
Explanation: The diagram above shows a way to make a pair of removals.
- The 1^st component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10.
- The 2^nd component has node [0] with value [1]. Its XOR value is 1 = 1.
- The 3^rd component has node [2] with value [5]. Its XOR value is 5 = 5.
The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9.
It can be shown that no other pair of removals will obtain a smaller score than 9.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png
Input: nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]
Output: 0
Explanation: The diagram above shows a way to make a pair of removals.
- The 1^st component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 = 0.
- The 2^nd component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 = 0.
- The 3^rd component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 = 0.
The score is the difference between the largest and smallest XOR value which is 0 - 0 = 0.
We cannot obtain a smaller score than 0.
Constraints:
•
n == nums.length•
3 <= n <= 1000•
1 <= nums[i] <= 10^8•
edges.length == n - 1•
edges[i].length == 2•
0 <= a_i, b_i < n•
a_i != b_i•
edges represents a valid tree.2025-07-25
3487. Maximum Unique Subarray Sum After Deletion
Topic: Array, Hash Table, Greedy
Difficulty: Easy
Problem:
You are given an integer array
You are allowed to delete any number of elements from
1. All elements in the subarray are unique.
2. The sum of the elements in the subarray is maximized.
Return the maximum sum of such a subarray.
Example 1:
Input: nums = 1,2,3,4,5
Output: 15
Explanation:
Select the entire array without deleting any element to obtain the maximum sum.
Example 2:
Input: nums = 1,1,0,1,1
Output: 1
Explanation:
Delete the element
Example 3:
Input: nums = 1,2,-1,-2,1,0,-1
Output: 3
Explanation:
Delete the elements
Constraints:
•
•
3487. Maximum Unique Subarray Sum After Deletion
Topic: Array, Hash Table, Greedy
Difficulty: Easy
Problem:
You are given an integer array
nums.You are allowed to delete any number of elements from
nums without making it empty. After performing the deletions, select a subarray of nums such that:1. All elements in the subarray are unique.
2. The sum of the elements in the subarray is maximized.
Return the maximum sum of such a subarray.
Example 1:
Input: nums = 1,2,3,4,5
Output: 15
Explanation:
Select the entire array without deleting any element to obtain the maximum sum.
Example 2:
Input: nums = 1,1,0,1,1
Output: 1
Explanation:
Delete the element
nums[0] == 1, nums[1] == 1, nums[2] == 0, and nums[3] == 1. Select the entire array [1] to obtain the maximum sum.Example 3:
Input: nums = 1,2,-1,-2,1,0,-1
Output: 3
Explanation:
Delete the elements
nums[2] == -1 and nums[3] == -2, and select the subarray [2, 1] from [1, 2, 1, 0, -1] to obtain the maximum sum.Constraints:
•
1 <= nums.length <= 100•
-100 <= nums[i] <= 1002025-07-26
3480. Maximize Subarrays After Removing One Conflicting Pair
Topic: Array, Segment Tree, Enumeration, Prefix Sum
Difficulty: Hard
Problem:
You are given an integer
Remove exactly one element from
Return the maximum number of subarrays possible after removing exactly one conflicting pair.
Example 1:
Input: n = 4, conflictingPairs = [2,3,1,4]
Output: 9
Explanation:
• Remove
• There are 9 subarrays in
• The maximum number of subarrays we can achieve after removing one element from
Example 2:
Input: n = 5, conflictingPairs = [1,2,2,5,3,5]
Output: 12
Explanation:
• Remove
• There are 12 subarrays in
• The maximum number of subarrays we can achieve after removing one element from
Constraints:
•
•
•
•
•
3480. Maximize Subarrays After Removing One Conflicting Pair
Topic: Array, Segment Tree, Enumeration, Prefix Sum
Difficulty: Hard
Problem:
You are given an integer
n which represents an array nums containing the numbers from 1 to n in order. Additionally, you are given a 2D array conflictingPairs, where conflictingPairs[i] = [a, b] indicates that a and b form a conflicting pair.Remove exactly one element from
conflictingPairs. Afterward, count the number of non-empty subarrays of nums which do not contain both a and b for any remaining conflicting pair [a, b].Return the maximum number of subarrays possible after removing exactly one conflicting pair.
Example 1:
Input: n = 4, conflictingPairs = [2,3,1,4]
Output: 9
Explanation:
• Remove
[2, 3] from conflictingPairs. Now, conflictingPairs = [[1, 4]].• There are 9 subarrays in
nums where [1, 4] do not appear together. They are [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3] and [2, 3, 4].• The maximum number of subarrays we can achieve after removing one element from
conflictingPairs is 9.Example 2:
Input: n = 5, conflictingPairs = [1,2,2,5,3,5]
Output: 12
Explanation:
• Remove
[1, 2] from conflictingPairs. Now, conflictingPairs = [[2, 5], [3, 5]].• There are 12 subarrays in
nums where [2, 5] and [3, 5] do not appear together.• The maximum number of subarrays we can achieve after removing one element from
conflictingPairs is 12.Constraints:
•
2 <= n <= 10^5•
1 <= conflictingPairs.length <= 2 * n•
conflictingPairs[i].length == 2•
1 <= conflictingPairs[i][j] <= n•
conflictingPairs[i][0] != conflictingPairs[i][1]2025-07-27
2210. Count Hills and Valleys in an Array
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index.
Return the number of hills and valleys in
Example 1:
Example 2:
Constraints:
•
•
2210. Count Hills and Valleys in an Array
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors of i are larger than nums[i]. Adjacent indices i and j are part of the same hill or valley if nums[i] == nums[j].Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index.
Return the number of hills and valleys in
nums.Example 1:
Input: nums = [2,4,1,1,6,5]
Output: 3
Explanation:
At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.
At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill.
At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.
At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.
At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.
At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley.
There are 3 hills and valleys so we return 3.
Example 2:
Input: nums = [6,6,5,5,4,1]
Output: 0
Explanation:
At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.
At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.
At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.
At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.
At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.
At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
There are 0 hills and valleys so we return 0.
Constraints:
•
3 <= nums.length <= 100•
1 <= nums[i] <= 1002025-07-28
2044. Count Number of Maximum Bitwise-OR Subsets
Topic: Array, Backtracking, Bit Manipulation, Enumeration
Difficulty: Medium
Problem:
Given an integer array
An array
The bitwise OR of an array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2044. Count Number of Maximum Bitwise-OR Subsets
Topic: Array, Backtracking, Bit Manipulation, Enumeration
Difficulty: Medium
Problem:
Given an integer array
nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.An array
a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.The bitwise OR of an array
a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).Example 1:
Input: nums = [3,1]
Output: 2
Explanation: The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:
- [3]
- [3,1]
Example 2:
Input: nums = [2,2,2]
Output: 7
Explanation: All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 2^3 - 1 = 7 total subsets.
Example 3:
Input: nums = [3,2,1,5]
Output: 6
Explanation: The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:
- [3,5]
- [3,1,5]
- [3,2,5]
- [3,2,1,5]
- [2,5]
- [2,1,5]
Constraints:
•
1 <= nums.length <= 16•
1 <= nums[i] <= 10^52025-07-29
2411. Smallest Subarrays With Maximum Bitwise OR
Topic: Array, Binary Search, Bit Manipulation, Sliding Window
Difficulty: Medium
Problem:
You are given a 0-indexed array
• In other words, let
The bitwise OR of an array is the bitwise OR of all the numbers in it.
Return an integer array
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Constraints:
•
•
•
2411. Smallest Subarrays With Maximum Bitwise OR
Topic: Array, Binary Search, Bit Manipulation, Sliding Window
Difficulty: Medium
Problem:
You are given a 0-indexed array
nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has the maximum possible bitwise OR.• In other words, let
B_ij be the bitwise OR of the subarray nums[i...j]. You need to find the smallest subarray starting at i, such that bitwise OR of this subarray is equal to max(B_ik) where i <= k <= n - 1.The bitwise OR of an array is the bitwise OR of all the numbers in it.
Return an integer array
answer of size n where answer[i] is the length of the minimum sized subarray starting at i with maximum bitwise OR.A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,0,2,1,3]
Output: [3,3,2,2,1]
Explanation:
The maximum possible bitwise OR starting at any index is 3.
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
Therefore, we return [3,3,2,2,1].
Example 2:
Input: nums = [1,2]
Output: [2,1]
Explanation:
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
Therefore, we return [2,1].
Constraints:
•
n == nums.length•
1 <= n <= 10^5•
0 <= nums[i] <= 10^92025-07-30
2419. Longest Subarray With Maximum Bitwise AND
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given an integer array
Consider a non-empty subarray from
• In other words, let
Return the length of the longest such subarray.
The bitwise AND of an array is the bitwise AND of all the numbers in it.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Example 2:
Constraints:
•
•
2419. Longest Subarray With Maximum Bitwise AND
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given an integer array
nums of size n.Consider a non-empty subarray from
nums that has the maximum possible bitwise AND.• In other words, let
k be the maximum value of the bitwise AND of any subarray of nums. Then, only subarrays with a bitwise AND equal to k should be considered.Return the length of the longest such subarray.
The bitwise AND of an array is the bitwise AND of all the numbers in it.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,3,2,2]
Output: 2
Explanation:
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
Example 2:
Input: nums = [1,2,3,4]
Output: 1
Explanation:
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^62025-07-31
898. Bitwise ORs of Subarrays
Topic: Array, Dynamic Programming, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
898. Bitwise ORs of Subarrays
Topic: Array, Dynamic Programming, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr.The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: arr = [0]
Output: 1
Explanation: There is only one possible result: 0.
Example 2:
Input: arr = [1,1,2]
Output: 3
Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.
Example 3:
Input: arr = [1,2,4]
Output: 6
Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
Constraints:
•
1 <= arr.length <= 5 * 10^4•
0 <= arr[i] <= 10^92025-08-01
118. Pascal's Triangle
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
Example 1:
Example 2:
Constraints:
•
118. Pascal's Triangle
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
numRows, return the first numRows of Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Constraints:
•
1 <= numRows <= 302025-08-02
2561. Rearranging Fruits
Topic: Array, Hash Table, Greedy, Sort
Difficulty: Hard
Problem:
You have two fruit baskets containing
• Chose two indices
• The cost of the swap is
Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.
Return the minimum cost to make both the baskets equal or
Example 1:
Example 2:
Constraints:
•
•
•
2561. Rearranging Fruits
Topic: Array, Hash Table, Greedy, Sort
Difficulty: Hard
Problem:
You have two fruit baskets containing
n fruits each. You are given two 0-indexed integer arrays basket1 and basket2 representing the cost of fruit in each basket. You want to make both baskets equal. To do so, you can use the following operation as many times as you want:• Chose two indices
i and j, and swap the ithfruit of basket1 with the jth fruit of basket2.• The cost of the swap is
min(basket1[i],basket2[j]).Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.
Return the minimum cost to make both the baskets equal or
-1 if impossible.Example 1:
Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2]
Output: 1
Explanation: Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
Example 2:
Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1]
Output: -1
Explanation: It can be shown that it is impossible to make both the baskets equal.
Constraints:
•
basket1.length == basket2.length•
1 <= basket1.length <= 10^5•
1 <= basket1[i],basket2[i] <= 10^92025-08-03
2106. Maximum Fruits Harvested After at Most K Steps
Topic: Array, Binary Search, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array
You are also given an integer
Return the maximum total number of fruits you can harvest.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/21/1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/21/2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/21/3.png
Constraints:
•
•
•
•
•
•
2106. Maximum Fruits Harvested After at Most K Steps
Topic: Array, Binary Search, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array
fruits where fruits[i] = [position_i, amount_i] depicts amount_i fruits at the position position_i. fruits is already sorted by position_i in ascending order, and each position_i is unique.You are also given an integer
startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.Return the maximum total number of fruits you can harvest.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/21/1.png
Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
Output: 9
Explanation:
The optimal way is to:
- Move right to position 6 and harvest 3 fruits
- Move right to position 8 and harvest 6 fruits
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/21/2.png
Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
Output: 14
Explanation:
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
The optimal way is to:
- Harvest the 7 fruits at the starting position 5
- Move left to position 4 and harvest 1 fruit
- Move right to position 6 and harvest 2 fruits
- Move right to position 7 and harvest 4 fruits
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/21/3.png
Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
Output: 0
Explanation:
You can move at most k = 2 steps and cannot reach any position with fruits.
Constraints:
•
1 <= fruits.length <= 10^5•
fruits[i].length == 2•
0 <= startPos, position_i <= 2 * 10^5•
position_i-1 < position_i for any i > 0 (0-indexed)•
1 <= amount_i <= 10^4•
0 <= k <= 2 * 10^52025-08-04
904. Fruit Into Baskets
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array
You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
• You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
• Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
• Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
Given the integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
904. Fruit Into Baskets
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array
fruits where fruits[i] is the type of fruit the i^th tree produces.You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
• You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
• Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
• Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
Given the integer array
fruits, return the maximum number of fruits you can pick.Example 1:
Input: fruits = [1,2,1]
Output: 3
Explanation: We can pick from all 3 trees.
Example 2:
Input: fruits = [0,1,2,2]
Output: 3
Explanation: We can pick from trees [1,2,2].
If we had started at the first tree, we would only pick from trees [0,1].
Example 3:
Input: fruits = [1,2,3,2,2]
Output: 4
Explanation: We can pick from trees [2,3,2,2].
If we had started at the first tree, we would only pick from trees [1,2].
Constraints:
•
1 <= fruits.length <= 10^5•
0 <= fruits[i] < fruits.length