4066. Maximum Equal Adjacent Pairs After at Most One Replacement
DifficultyMedium
Description
You are given a 1-indexed integer array nums.
You can choose two distinct values x and y and perform the following operation at most once:
- Replace every occurrence of
xinnumswithy.
Return the maximum possible number of pairs of adjacent elements that are equal after performing the operation.
Example 1:
Input: nums = [1,2,3,2]
Output: 2
Explanation:
- One optimal solution is to choose
x = 3andy = 2. - The resulting array is
[1, 2, 2, 2]. - There are 2 pairs of adjacent elements that are equal:
(nums[2], nums[3])and(nums[3], nums[4]). - Therefore, the answer is 2.
Example 2:
Input: nums = [1,2,1,2,1]
Output: 4
Explanation:
- One optimal solution is to choose
x = 1andy = 2. - The resulting array is
[2, 2, 2, 2, 2]. - There are 4 pairs of adjacent elements that are equal:
(nums[1], nums[2]),(nums[2], nums[3]),(nums[3], nums[4]), and(nums[4], nums[5]). - Therefore, the answer is 4.
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation:
- One optimal solution is to perform no operation.
- Thus, the resulting array is
[1, 1, 1]. - There are 2 pairs of adjacent elements that are equal:
(nums[1], nums[2])and(nums[2], nums[3]). - Therefore, the answer is 2.
Constraints:
2 <= nums.length <= 1051 <= nums[i] <= 109
Solutions
Solution 1: Hash Map
Thinking
\(n\) can be \(10^5\) and values can be \(10^9\). Trying every distinct pair \(x,y\), replacing, and recounting adjacent equals is too many candidates.
An adjacent pair that is already equal stays equal no matter which value is replaced by another. One replacement only equalizes adjacent positions whose values are exactly that pair \(x,y\). A different unordered pair is a different operation.
Count the adjacent positions that are already equal, then count unequal adjacent positions by unordered pair, and add the largest of those counts. Doing nothing corresponds to a maximum of \(0\).
Adjacent positions that are already equal stay equal after any replacement: if both hold \(x\), both become \(y\), and every other value is unchanged. Let \(\textit{ans}\) be the number of such pairs.
One operation picks two distinct values and replaces every occurrence of one with the other. An adjacent pair that newly becomes equal must already have been exactly those two values. For an unequal adjacent pair \(x,y\), place the smaller value first and encode
Since \(x,y\le 10^9\), the key fits in a \(64\)-bit integer. \(\textit{cnt}[\textit{key}]\) is how often that pair occurs in adjacent positions. Over every candidate operation, the number of newly equal adjacent pairs is the maximum of these counts, \(\textit{mx}\). Skipping the operation leaves \(\textit{mx}=0\). The answer is \(\textit{ans}+\textit{mx}\).
The time complexity is \(O(n)\) and the space complexity is \(O(n)\).
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 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |