3397. Maximum Number of Distinct Elements After Operations
Description
You are given an integer array nums
and an integer k
.
You are allowed to perform the following operation on each element of the array at most once:
- Add an integer in the range
[-k, k]
to the element.
Return the maximum possible number of distinct elements in nums
after performing the operations.
Example 1:
Input: nums = [1,2,2,3,3,4], k = 2
Output: 6
Explanation:
nums
changes to [-1, 0, 1, 2, 3, 4]
after performing operations on the first four elements.
Example 2:
Input: nums = [4,4,4,4], k = 1
Output: 3
Explanation:
By adding -1 to nums[0]
and 1 to nums[1]
, nums
changes to [3, 5, 4, 4]
.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
0 <= k <= 109
Solutions
Solution 1: Greedy + Sorting
We can sort the array $\textit{nums}$ and then consider each element $x$ from left to right.
For the first element, we can greedily change it to $x - k$, making $x$ as small as possible to leave more space for subsequent elements. We use the variable $\textit{pre}$ to track the maximum value of the elements used so far, initialized to negative infinity.
For subsequent elements $x$, we can greedily change it to $\min(x + k, \max(x - k, \textit{pre} + 1))$. Here, $\max(x - k, \textit{pre} + 1)$ means we try to make $x$ as small as possible but not smaller than $\textit{pre} + 1$. If this value exists and is less than $x + k$, we can change $x$ to this value, increment the count of distinct elements, and update $\textit{pre}$ to this value.
After traversing the array, we obtain the maximum number of distinct elements.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|