📌  相关文章
📜  通过用它们的总和重复替换不相等的相邻数组元素对来最小化数组长度

📅  最后修改于: 2021-10-27 06:25:16             🧑  作者: Mango

给定一个整数数组arr[],任务是通过用它们的和重复替换两个不相等的相邻数组元素来最小化给定数组的长度。一旦数组减少到它的最小可能长度,即数组中没有相邻的不相等对,打印所需的操作计数。

例子:

朴素方法:解决问题的最简单方法是遍历给定数组,并为每个相邻的不等对用其总和替换该对。最后,如果数组中不存在不等对,则打印数组的长度。
时间复杂度: 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


Javascript


输出:
1

时间复杂度: O(N)
辅助空间:O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程