📜  将数组拆分为最大子数组,以便每个不同的元素都位于单个子数组中

📅  最后修改于: 2021-05-19 18:46:08             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是将数组拆分为最大数量的子数组,以使所有不同数组元素的第一个和最后一个出现在单个子数组中。

例子:

方法:想法是使用哈希存储每个数组元素最后一次出现的索引。请按照以下步骤解决问题:

  1. 初始化一个数组,用hash []表示,以存储每个数组元素最后一次出现的索引。
  2. 遍历数组,并检查当前子数组的所有先前元素的最后一次出现的最大索引是否小于或等于当前索引,然后将计数加1。
  3. 最后,打印count的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to maximize the
// count of subarrays
int maxCtSubarrays(int arr[], int N)
{
    // Store the last index of
    // every array element
    int hash[1000001] = { 0 };
 
    for (int i = 0; i < N; i++) {
        hash[arr[i]] = i;
    }
 
    // Store the maximum index of the
    // last occurrence of all elements
    int maxIndex = -1;
 
    // Store the count of subarrays
    int res = 0;
 
    for (int i = 0; i < N; i++) {
        maxIndex = max(maxIndex,
                       hash[arr[i]]);
 
        // If maximum of last indices
        // previous elements is equal
        // to the current index
        if (maxIndex == i) {
            res++;
        }
    }
 
    // Return the count
    // of subarrays
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4, 1,
                  4, 7, 7, 8 };
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    cout << maxCtSubarrays(arr, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
 
// Function to maximize the
// count of subarrays
static int maxCtSubarrays(int arr[],
                          int N)
{
  // Store the last index of
  // every array element
  int hash[] = new int[1000001];
 
  for (int i = 0; i < N; i++)
  {
    hash[arr[i]] = i;
  }
 
  // Store the maximum index of the
  // last occurrence of all elements
  int maxIndex = -1;
 
  // Store the count of subarrays
  int res = 0;
 
  for (int i = 0; i < N; i++)
  {
    maxIndex = Math.max(maxIndex,
                        hash[arr[i]]);
 
    // If maximum of last indices
    // previous elements is equal
    // to the current index
    if (maxIndex == i)
    {
      res++;
    }
  }
 
  // Return the count
  // of subarrays
  return res;
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {1, 2, 4, 1,
               4, 7, 7, 8};
  int N = arr.length;
  System.out.print(maxCtSubarrays(arr, N));
}
}
 
// This code is contributed by Chitranayal


Python3
# Python3 program to implement
# the above approach
 
# Function to maximize the
# count of subarrays
def maxCtSubarrays(arr, N):
     
    # Store the last index of
    # every array element
    hash = [0] * (1000001)
 
    for i in range(N):
        hash[arr[i]] = i
 
    # Store the maximum index of the
    # last occurrence of all elements
    maxIndex = -1
 
    # Store the count of subarrays
    res = 0
 
    for i in range(N):
        maxIndex = max(maxIndex,
                       hash[arr[i]])
 
        # If maximum of last indices
        # previous elements is equal
        # to the current index
        if (maxIndex == i):
            res += 1
 
    # Return the count
    # of subarrays
    return res
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 4, 1,
            4, 7, 7, 8 ]
    N = len(arr)
 
    print(maxCtSubarrays(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG {
 
// Function to maximize the
// count of subarrays
static int maxCtSubarrays(int []arr,
                          int N)
{
  // Store the last index of
  // every array element
  int []hash = new int[1000001];
 
  for (int i = 0; i < N; i++)
  {
    hash[arr[i]] = i;
  }
 
  // Store the maximum index of the
  // last occurrence of all elements
  int maxIndex = -1;
 
  // Store the count of subarrays
  int res = 0;
 
  for (int i = 0; i < N; i++)
  {
    maxIndex = Math.Max(maxIndex,
                        hash[arr[i]]);
 
    // If maximum of last indices
    // previous elements is equal
    // to the current index
    if (maxIndex == i)
    {
      res++;
    }
  }
 
  // Return the count
  // of subarrays
  return res;
}
 
// Driver Code
public static void Main(String[] args)
{
  int []arr = {1, 2, 4, 1,
               4, 7, 7, 8};
  int N = arr.Length;
  Console.Write(maxCtSubarrays(arr, N));
}
}
 
// This code is contributed by Princi Singh


输出
3

时间复杂度: O(N)
辅助空间: O(X)其中X是给定数组中的最大元素。