Welcome to Subscribe On Youtube
4083. Minimum Deletions to Make Array Sharp
Description
You are given an integer array nums.
You may delete any number of elements from nums. The remaining elements keep their original order.
The resulting array a is sharp if the bitwise XOR of every pair of adjacent elements is non-zero. That is, a[i] XOR a[i + 1] != 0 for every index i such that 0 <= i < a.length - 1. An array with at most one element is sharp.
Return the minimum number of deletions needed to make nums sharp.
Bitwise XOR compares the corresponding bits of two integers. A bit in the result is 1 if the corresponding bits differ, and 0 otherwise.
Example 1:
Input: nums = [1,1,2,2,2,3]
Output: 3
Explanation:
- The pairs at indices
(0, 1),(2, 3), and(3, 4)each have an XOR of 0. - Deleting one 1 and two 2s leaves
[1, 2, 3], where1 XOR 2 == 3and2 XOR 3 == 1are both non-zero. Therefore, the answer is 3.
Example 2:
Input: nums = [4,4,9,4]
Output: 1
Explanation:
- The pair at indices
(0, 1)has an XOR of 0. - Deleting either of these two 4s leaves
[4, 9, 4], where4 XOR 9 == 13and9 XOR 4 == 13are both non-zero.
Constraints:
2 <= nums.length <= 1001 <= nums[i] <= 100
Solutions
Solution 1: Count Adjacent Equals
Thinking
$n$ is at most $100$, so comparing adjacent elements is cheap, but enumerating every deletion subset is exponential. Adjacent XOR is $0$ exactly when the two values are equal, so searching subsets is unnecessary.
The bottleneck is a contiguous run of equal values. Relative order is fixed, so a run can keep only one element. Runs of different values are already separated by an unequal boundary, and deleting the extras inside a run does not make those runs interfere.
A run of length $k$ costs $k-1$ deletions, and it contains exactly $k-1$ adjacent equal pairs. One left-to-right scan that counts positions where $\textit{nums}[i]=\textit{nums}[i-1]$ is the minimum number of deletions.
Two integers have bitwise XOR $0$ if and only if they are equal. The task is therefore to delete as few elements as possible, without changing the relative order, so that every pair of adjacent remaining elements is different.
Scan the array from left to right. If $\textit{nums}[i]=\textit{nums}[i-1]$, those two positions cannot both stay. Treat each contiguous run of equal values as one group. A group of length $k$ must lose $k-1$ elements, and that group contains exactly $k-1$ adjacent equal pairs. Elements on either side of a group boundary are already different, so keeping one element from each group still leaves unequal neighbors. The minimum number of deletions is the number of adjacent equal pairs.
The time complexity is $O(n)$, and the space complexity is $O(1)$, where $n$ is the length of the array.
-
class Solution { public int minDeletions(int[] nums) { int ans = 0; for (int i = 1; i < nums.length; ++i) { if (nums[i] == nums[i - 1]) { ++ans; } } return ans; } } -
class Solution { public: int minDeletions(vector<int>& nums) { int ans = 0; for (int i = 1; i < nums.size(); ++i) { if (nums[i] == nums[i - 1]) { ++ans; } } return ans; } }; -
class Solution: def minDeletions(self, nums: list[int]) -> int: return sum(x == y for x, y in pairwise(nums)) -
func minDeletions(nums []int) (ans int) { for i := 1; i < len(nums); i++ { if nums[i] == nums[i-1] { ans++ } } return } -
function minDeletions(nums: number[]): number { let ans: number = 0; for (let i = 1; i < nums.length; ++i) { if (nums[i] === nums[i - 1]) { ++ans; } } return ans; }