📜  将给定数组拆分为最少数量的子数组,以便重新排列子数组的顺序对数组进行排序

📅  最后修改于: 2022-05-13 01:56:08.988000             🧑  作者: Mango

将给定数组拆分为最少数量的子数组,以便重新排列子数组的顺序对数组进行排序

给定一个由N个整数组成的数组arr[] ,任务是找到将数组元素拆分为子数组的最小数量,以便重新排列子数组的顺序对给定数组进行排序。

例子:

方法:给定的问题可以通过维护数组arr[]的排序版本并将原始数组中与排序数组中以相同顺序出现的所有整数组合在一起来解决。以下是步骤:

  • 维护一个对V的向量,该向量存储给定数组中所有元素的当前元素的值和数组arr[]的当前元素的索引。
  • 对向量V进行排序。
  • 初始化一个变量,例如cnt1 ,它存储所需的最小子数组数。
  • 遍历[1, N – 1]范围内i的向量V并执行以下步骤:
    • 如果原始数组中第i元素的索引是原始数组中的(1 + 第 (i – 1)元素的索引),则可以将两者组合在同一个子数组中。
    • 否则,这两个元素需要位于单独的子数组中,并将cnt的值增加1
  • 完成上述步骤后,打印cnt的值作为可能破坏子数组的结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
 
#include 
using namespace std;
 
// Function to find minimum number of
// subarrays such that rearranging the
// subarrays sort the array
int numberOfSubarrays(int arr[], int n)
{
    // Stores the minimum number of
    // subarrays
    int cnt = 1;
 
    // Stores all the elements in the
    // array with their indices
    vector > v(n);
 
    // Copy array elements in vector
    for (int i = 0; i < n; i++) {
        v[i].first = arr[i];
        v[i].second = i;
    }
 
    // Sort the vector v
    sort(v.begin(), v.end());
 
    // Iterate through vector v
    for (int i = 1; i < n; i++) {
 
        // If the (i)th and (i-1)th element
        // can be grouped in same subarray
        if (v[i].second == v[i - 1].second + 1) {
            continue;
        }
        else {
            cnt++;
        }
    }
 
    // Return resultant count
    return cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 6, 3, 4, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << numberOfSubarrays(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
    static class pair
    {
        int first, second;
        public pair(int first, int second) 
        {
            this.first = first;
            this.second = second;
        }   
    }
   
// Function to find minimum number of
// subarrays such that rearranging the
// subarrays sort the array
static int numberOfSubarrays(int arr[], int n)
{
   
    // Stores the minimum number of
    // subarrays
    int cnt = 1;
 
    // Stores all the elements in the
    // array with their indices
    pair[] v = new pair[n];
 
    // Copy array elements in vector
    for (int i = 0; i < n; i++) {
        v[i] = new pair(0,0);
        v[i].first = arr[i];
        v[i].second = i;
    }
 
    // Sort the vector v
    Arrays.sort(v,(a,b)->a.first-b.first);
 
    // Iterate through vector v
    for (int i = 1; i < n; i++) {
 
        // If the (i)th and (i-1)th element
        // can be grouped in same subarray
        if (v[i].second == v[i - 1].second + 1) {
            continue;
        }
        else {
            cnt++;
        }
    }
 
    // Return resultant count
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 6, 3, 4, 2, 1 };
    int N = arr.length;
    System.out.print(numberOfSubarrays(arr, N));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python Program to implement
# the above approach
 
# Function to find minimum number of
# subarrays such that rearranging the
# subarrays sort the array
def numberOfSubarrays(arr, n):
   
    # Stores the minimum number of
    # subarrays
    cnt = 1
 
    # Stores all the elements in the
    # array with their indices
    v = []
 
    # Copy array elements in vector
    for i in range(n):
        v.append({'first': arr[i], 'second': i})
     
    # Sort the vector v
    v = sorted(v, key = lambda i: i['first'])
 
    # Iterate through vector v
    for i in range(1, n):
 
        # If the (i)th and (i-1)th element
        # can be grouped in same subarray
        if (v[i]['first'] == v[i - 1]['second']):
            continue
        else:
            cnt += 1
         
    # Return resultant count
    return cnt
 
# Driver Code
arr = [6, 3, 4, 2, 1]
N = len(arr)
print(numberOfSubarrays(arr, N))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
    class pair : IComparable
    {
        public int first, second;
        public pair(int first, int second) 
        {
            this.first = first;
            this.second = second;
        }  
        public int CompareTo(pair other)
        {
            // return other.Salary.CompareTo(this.Salary);
            if (this.first < other.first)
            {
                return 1;
            }
            else if (this.first > other.first)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }
   
// Function to find minimum number of
// subarrays such that rearranging the
// subarrays sort the array
static int numberOfSubarrays(int []arr, int n)
{
   
    // Stores the minimum number of
    // subarrays
    int cnt = 1;
 
    // Stores all the elements in the
    // array with their indices
    pair[] v = new pair[n];
 
    // Copy array elements in vector
    for (int i = 0; i < n; i++) {
        v[i] = new pair(0,0);
        v[i].first = arr[i];
        v[i].second = i;
    }
 
    // Sort the vector v
    Array.Sort(v);
 
    // Iterate through vector v
    for (int i = 1; i < n; i++) {
 
        // If the (i)th and (i-1)th element
        // can be grouped in same subarray
        if (v[i].second == v[i - 1].second + 1) {
            continue;
        }
        else {
            cnt++;
        }
    }
 
    // Return resultant count
    return cnt;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 6, 3, 4, 2, 1 };
    int N = arr.Length;
    Console.Write(numberOfSubarrays(arr, N));
 
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
4

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