最小化删除,使得没有偶数索引数组元素与下一个元素相同
给定一个数组arr[] ,任务是找到所需的最小删除操作数,使得:
- 新创建的数组应该有一个偶数长度。
- arr[i] ≠ arr[i+1]对于每个i其中i%2==0 。
例子:
Input: arr[] = {1, 1, 2, 3, 5}
Output: 1
Explanation: Delete the first or second element of the array to satisfy the conditons.
Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: 2
Explanation: Delete first element as the next element is a duplicate and the current index is even.
Need to delete another element from the newly created array because
the size of the newly created array is odd. arr = {1, 2, 2, 3, 3}
Delete the last element to make its length even. So the total number of operations is 2.
方法:解决这个问题的一般思路是:
Maximize the number of elements in the newly created array and keep on checking if any even index element has the same value as the one just next to it.
上面的思路可以用栈来实现,生成新的数组。按照下面提到的步骤来实现上述观察:
- 创建一组对以将元素和元素的索引存储在新数组中。
- 遍历数组arr[]从i = 0 到 N-1 :
- 如果栈顶元素的索引是奇数,则将当前元素连同它的索引一起压入栈中的新数组。
- 否则,检查arr[i]的值和最上面的元素是否相同。
- 如果它们相同,则继续arr[]的下一个元素。
- 否则将当前元素添加到新数组中。指向栈的索引指针。
- 最后,如果堆栈的大小是奇数,则从堆栈中再删除一个元素。
- 返回N 的值 - 堆栈大小作为答案,因为这是所需的最小删除次数。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the minimum deletions
int minOperations(vector& arr)
{
int n = arr.size();
// Stack that will maintain the newly
// created array
stack > st;
int k = 1;
// Pushed the first element to the stack.
st.push({ arr[0], 0 });
for (int i = 1; i < n; i++) {
// If the top most element in the
// stack is at even index and the
// element is same as the curernt
// array element then continue.
if (st.top().second % 2 == 0
&& st.top().first == arr[i]) {
continue;
}
// If the top most element in the stack
// is at odd index or the two elements
// are not same then push the current
// element to the stack.
else {
st.push({ arr[i], k });
k++;
}
}
// Find the stack size
int s = st.size();
// If stack size is odd then
// delete further one element
// from stack, hence return
// array size - stack size +1.
if (s & 1 == 1) {
return n - s + 1;
}
// Return (array size - stack size).
return n - s;
}
// Driver code
int main()
{
vector arr = { 1, 1, 2, 3, 5 };
// Function call
cout << minOperations(arr);
return 0;
}
Python3
# Python3 program for above approach
# Function to find the minimum deletions
def minOperations(arr):
n = len(arr)
# stack that will maintain
# the newly created array
st = []
k = 1
# Pushed the first element to the stack
st.append([arr[0], 0])
for i in range(1, n):
# If the top most element in the
# stack is at even index and the
# element is same as the curernt
# array element then continue
if st[len(st) - 1][1] % 2 == 0 and st[len(st) - 1][0] == arr[i]:
continue
# If the top most element in the stack
# is at odd index or the two elements
# are not same then push the current
# element to the stack.
else:
st.append([arr[i], k])
k += 1
# Find the stack size
s = len(st)
# If stack size is odd then
# delete further one element
# from stack, hence return
# array size - stack size +1.
if s & 1 == 1:
return n - s + 1
# Return (array size - stack size).
return n - s
# Driver code
arr = [1, 1, 2, 3, 5]
# Function call
print(minOperations(arr))
# This code is contributed by phasing17
Javascript
1
时间复杂度: O(N)
辅助空间: O(N)