给定一个整数数组arr [],任务是通过用其和重复替换两个不相等的相邻数组元素,以最小化给定数组的长度。一旦将阵列减小到其最小可能长度,即阵列中没有剩余的不相等对,请打印所需的操作计数。
例子:
Input: arr[] = {2, 1, 3, 1}
Output: 1
Explanation:
Operation 1: {2, 1, 3, 1} -> {3, 3, 1}
Operation 2: {3, 3, 1} -> {3, 4}
Operation 3: {3, 4} -> {7}
Therefore, the minimum length the array can be reduced to is 1.
Input: arr[] = {1, 1, 1, 1}
Output: 4
Explanation:
No merge operation is possible as no unequal adjacent pair can be obtained.
Hence, the minimum length of the array is 4.
天真的方法:解决问题的最简单方法是遍历给定的数组,对于每个相邻的不相等对,用其和替换该对。最后,如果数组中不存在不相等的对,则打印数组的长度。
时间复杂度: O(N 2 )
辅助空间:O(N)
高效的方法:可以基于以下观察来优化上述方法:
- 如果数组的所有元素均相等,则无法执行任何操作。因此,打印N ,即数组的初始长度,作为数组的最小可缩减长度
- 否则,数组的最小长度将始终为1 。
因此,要解决该问题,只需遍历数组并检查所有数组元素是否相等。如果发现是正确的,则将N打印为必需的答案。否则,打印1 。
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
using namespace std;
// Function that returns the minimum
// length of the array after merging
// unequal adjacent elements
int minLength(int A[], int N)
{
// Stores the first element
// and its frequency
int elem = A[0], count = 1;
// Traverse the array
for (int i = 1; i < N; i++) {
if (A[i] == elem) {
count++;
}
else {
break;
}
}
// If all elements are equal
if (count == N)
// No merge-pair operations
// can be performed
return N;
// Otherwise
else
return 1;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 1, 3, 1 };
// Length of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << minLength(arr, N) << endl;
return 0;
}
Java
// Java program for
// the above approach
class GFG{
// Function that returns the minimum
// length of the array
// after merging unequal
// adjacent elements
static int minLength(int A[],
int N)
{
// Stores the first element
// and its frequency
int elem = A[0], count = 1;
// Traverse the array
for (int i = 1; i < N; i++)
{
if (A[i] == elem)
{
count++;
}
else
{
break;
}
}
// If all elements are equal
if (count == N)
// No merge-pair operations
// can be performed
return N;
// Otherwise
else
return 1;
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = {2, 1, 3, 1};
// Length of the array
int N = arr.length;
// Function Call
System.out.print(minLength(arr, N) + "\n");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function that returns the minimum
# length of the array after merging
# unequal adjacent elements
def minLength(A, N):
# Stores the first element
# and its frequency
elem = A[0]
count = 1
# Traverse the array
for i in range(1, N):
if (A[i] == elem):
count += 1
else:
break
# If all elements are equal
if (count == N):
# No merge-pair operations
# can be performed
return N
# Otherwise
else:
return 1
# Driver Code
# Given array
arr = [ 2, 1, 3, 1 ]
# Length of the array
N = len(arr)
# Function call
print(minLength(arr, N))
# This code is contributed by code_hunt
C#
// C# program for
// the above approach
using System;
class GFG{
// Function that returns the minimum
// length of the array
// after merging unequal
// adjacent elements
static int minLength(int []A,
int N)
{
// Stores the first element
// and its frequency
int elem = A[0], count = 1;
// Traverse the array
for (int i = 1; i < N; i++)
{
if (A[i] == elem)
{
count++;
}
else
{
break;
}
}
// If all elements are equal
if (count == N)
// No merge-pair operations
// can be performed
return N;
// Otherwise
else
return 1;
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = {2, 1, 3, 1};
// Length of the array
int N = arr.Length;
// Function Call
Console.Write(minLength(arr, N) + "\n");
}
}
// This code is contributed by Rajput-Ji
1
时间复杂度: O(N)
辅助空间:O(1)