给定一个大小为N的数组arr[] ,任务是通过从由相同单个元素组成的数组的开头和结尾重复删除子数组来最小化给定数组的长度。
例子:
Input: arr[] = { 3, 1, 2, 1, 1, 2, 1, 3 }
Output: 0
Explanation:
- Since both the first and last elements are 3, removing them modifies arr[] to {1, 2, 1, 1, 2, 1}.
- Since both the first and last elements are 1, removing them modifies arr[] to {2, 1, 1, 2}.
- Since both the first and last elements are 2, removing them modifies arr[] to {1, 1}.
- Since both the first and last elements are 1, removing them modifies arr[] to {}.
Input: arr[] = {1, 1, 2, 3, 3, 1, 2, 2, 1}
Output: 3
Explanation:
- Removing { 1, 1 } from the start and { 1 } from the end modifies arr[] to { 2, 3, 3, 1, 2, 2 }.
- Removing { 2 } from the start and { 2, 2 } from the end modifies arr[] to { 3, 3, 1 }.
- No more elements can be deleted.
方法:想法是使用两点技术来解决问题。请按照以下步骤解决问题:
- 初始化两个指针front = 0 , back = N – 1以同时从两端遍历数组。
- 遍历数组arr[]直到front < back:
- 如果两个元素不同,则中断循环。
- 否则,递增前指针并递减后指针,直到它们指向与当前元素不同的元素。
- 打印两个指针位置之间的差异作为数组的最小长度。
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
using namespace std;
// Function to minimize length of
// the array by removing similar
// subarrays from both ends of the array
void findMinLength(int arr[], int N)
{
// Initialize two pointers
int front = 0, back = N - 1;
while (front < back) {
// Stores the current integer
int x = arr[front];
// Check if the elements at
// both ends are same or not
if (arr[front] != arr[back])
break;
// Move the front pointer
while (arr[front] == x
&& front <= back)
front++;
// Move the rear pointer
while (arr[back] == x
&& front <= back)
back--;
}
// Print the minimized length of the array
cout << back - front + 1 << endl;
}
// Driver Code
int main()
{
// Input
int arr[] = { 1, 1, 2, 3, 3, 1, 2, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to find the
// minimized length of the array
findMinLength(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to minimize length of
// the array by removing similar
// subarrays from both ends of the array
static void findMinLength(int arr[], int N)
{
// Initialize two pointers
int front = 0, back = N - 1;
while (front < back) {
// Stores the current integer
int x = arr[front];
// Check if the elements at
// both ends are same or not
if (arr[front] != arr[back])
break;
// Move the front pointer
while (arr[front] == x
&& front <= back)
front++;
// Move the rear pointer
while (arr[back] == x
&& front <= back)
back--;
}
// Print the minimized length of the array
System.out.println( back - front + 1 );
}
// Driver Code
public static void main(String[] args)
{
// Input
int arr[] = { 1, 1, 2, 3, 3, 1, 2, 2, 1 };
int N = arr.length;
// Function call to find the
// minimized length of the array
findMinLength(arr, N);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python program for
# the above approach
# Function to minimize length of
# the array by removing similar
# subarrays from both ends of the array
def findMinLength(arr, N):
# Initialize two pointers
front = 0
back = N - 1
while (front < back):
# Stores the current integer
x = arr[front]
# Check if the elements at
# both ends are same or not
if arr[front] != arr[back]:
break
# Move the front pointer
while (arr[front] == x and front <= back):
front += 1
# Move the rear pointer
while (arr[back] == x and front <= back):
back -= 1
# Print the minimized length of the array
print(back - front + 1)
# Driver Code
# Input
arr = [1, 1, 2, 3, 3, 1, 2, 2, 1]
N = len(arr)
# Function call to find the
# minimized length of the array
findMinLength(arr, N)
# This code is contributed by sudhanshugupta2019a.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to minimize length of
// the array by removing similar
// subarrays from both ends of the array
static void findMinLength(int []arr, int N)
{
// Initialize two pointers
int front = 0, back = N - 1;
while (front < back)
{
// Stores the current integer
int x = arr[front];
// Check if the elements at
// both ends are same or not
if (arr[front] != arr[back])
break;
// Move the front pointer
while (arr[front] == x
&& front <= back)
front++;
// Move the rear pointer
while (arr[back] == x
&& front <= back)
back--;
}
// Print the minimized length of the array
Console.WriteLine( back - front + 1 );
}
// Driver Code
public static void Main(String[] args)
{
// Input
int []arr = { 1, 1, 2, 3, 3, 1, 2, 2, 1 };
int N = arr.Length;
// Function call to find the
// minimized length of the array
findMinLength(arr, N);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live