3041 - Maximize Consecutive Elements in an Array After Modification (Hard)
Problem Link
https://leetcode.com/problems/maximize-consecutive-elements-in-an-array-after-modification
Problem Statement
You are given a 0-indexed array nums consisting of positive integers.
Initially, you can increase the value of any element in the array by at most 1.
After that, you need to select one or more elements from the final array such that those elements are consecutive when sorted in increasing order. For example, the elements [3, 4, 5] are consecutive while [3, 4, 6] and [1, 1, 2, 3] are not.
Return the maximum number of elements that you can select.
Example 1:
Input: nums = [2,1,5,1,1]
Output: 3
Explanation: We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
We select the elements [3,1,5,2,1] and we sort them to obtain [1,2,3], which are consecutive.
It can be shown that we cannot select more than 3 consecutive elements.Example 2:
Input: nums = [1,4,7,10]
Output: 1
Explanation: The maximum consecutive elements that we can select is 1.Constraints:
1 <= nums.length <= 1e51 <= nums[i] <= 1e6
Approach 1: Dynamic Programming
Each element can either stay the same or increase by . We want to choose as many elements as possible so that, after sorting the chosen elements, every number is exactly one greater than the previous number. Duplicate values cannot appear in the chosen sequence.
First, we sort . This lets us process smaller numbers before larger numbers and build consecutive sequences by adding elements to their ends.
We use to store the longest consecutive sequence ending at each value. For example, if , we can form a sequence of length ending at , namely . A returns for missing keys, meaning that no sequence ending at that value has been found yet.
For each number , we first consider increasing it to . It can extend a sequence ending at , so we set . Then we consider leaving it unchanged. It can extend a sequence ending at , so we set .
The order of these updates is important. We update before so that the increased option uses a sequence built only from earlier elements. Otherwise, we might accidentally use the current element twice.
The time complexity is and the space complexity is .
class Solution:
def maxSelectedElements(self, nums: List[int]) -> int:
dp = defaultdict(int)
res = 0
for x in sorted(nums):
dp[x + 1] = dp[x] + 1
dp[x] = dp[x - 1] + 1
res = max(res, dp[x], dp[x + 1])
return res3035 - Maximum Palindromes After Operations
Maximize how many words can become palindromes after swaps by counting available character pairs and prioritizing shorter words.
3064 - Guess the Number Using Bitwise Questions I
Recover a hidden number by querying shared set bits with masks that isolate individual binary positions.