📌  相关文章
📜  最小化获取数组排序顺序所需的步骤

📅  最后修改于: 2021-10-28 01:41:42             🧑  作者: Mango

给定一个由整数[1, N]排列组成的数组arr[] ,通过重新排列排序顺序[1, N]得出,任务是找到排序顺序[1, N]之后的最小步骤数重复,通过重复相同的过程,通过该过程从每一步的排序序列中获得 arr[]。

例子:

方法:
这个问题可以简单地通过使用直接寻址的概念来解决。请按照以下步骤解决问题:

  • 初始化一个数组dat[]以进行直接寻址。
  • 迭代[1, N]并计算每个元素的当前索引与其在排序序列中的索引的差。
  • 计算数组dat[]的 LCM。
  • 现在,打印获得的 LCM 作为获得排序顺序所需的最少步骤

下面是上述方法的实现:

C++14
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find
// GCD of two numbers
int gcd(int a, int b)
{
    if (b == 0)
        return a;
 
    return gcd(b, a % b);
}
 
// Function to calculate the
// LCM of array elements
int findlcm(int arr[], int n)
{
    // Initialize result
    int ans = 1;
 
    for (int i = 1; i <= n; i++)
        ans = (((arr[i] * ans))
            / (gcd(arr[i], ans)));
 
    return ans;
}
 
// Function to find minimum steps
// required to obtain sorted sequence
void minimumSteps(int arr[], int n)
{
 
    // Inititalize dat[] array for
    // Direct Address Table.
    int i, dat[n + 1];
 
    for (i = 1; i <= n; i++)
 
        dat[arr[i - 1]] = i;
 
    int b[n + 1], j = 0, c;
 
    // Calculating steps required
    // for each element to reach
    // its sorted position
    for (i = 1; i <= n; i++) {
        c = 1;
        j = dat[i];
        while (j != i) {
            c++;
            j = dat[j];
        }
        b[i] = c;
    }
 
    // Calculate LCM of the array
    cout << findlcm(b, n);
}
 
// Driver Code
int main()
{
 
    int arr[] = { 5, 1, 4, 3, 2, 7, 6 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minimumSteps(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
     
// Function to find
// GCD of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
 
    return gcd(b, a % b);
}
 
// Function to calculate the
// LCM of array elements
static int findlcm(int arr[], int n)
{
     
    // Initialize result
    int ans = 1;
 
    for(int i = 1; i <= n; i++)
        ans = (((arr[i] * ans)) /
            (gcd(arr[i], ans)));
 
    return ans;
}
 
// Function to find minimum steps
// required to obtain sorted sequence
static void minimumSteps(int arr[], int n)
{
 
    // Inititalize dat[] array for
    // Direct Address Table.
    int i;
    int dat[] = new int[n + 1];
 
    for(i = 1; i <= n; i++)
        dat[arr[i - 1]] = i;
 
    int b[] = new int[n + 1];
    int j = 0, c;
 
    // Calculating steps required
    // for each element to reach
    // its sorted position
    for(i = 1; i <= n; i++)
    {
        c = 1;
        j = dat[i];
         
        while (j != i)
        {
            c++;
            j = dat[j];
        }
        b[i] = c;
    }
 
    // Calculate LCM of the array
    System.out.println(findlcm(b, n));
}
 
// Driver code   
public static void main(String[] args)
{
    int arr[] = { 5, 1, 4, 3, 2, 7, 6 };
 
    int N = arr.length;
 
    minimumSteps(arr, N);
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program to implement
# the above approach
 
# Function to find
# GCD of two numbers
def gcd(a, b):
 
    if(b == 0):
        return a
 
    return gcd(b, a % b)
 
# Function to calculate the
# LCM of array elements
def findlcm(arr, n):
 
    # Initialize result
    ans = 1
 
    for i in range(1, n + 1):
        ans = ((arr[i] * ans) //
            (gcd(arr[i], ans)))
 
    return ans
 
# Function to find minimum steps
# required to obtain sorted sequence
def minimumSteps(arr, n):
 
    # Inititalize dat[] array for
    # Direct Address Table.
    dat = [0] * (n + 1)
 
    for i in range(1, n + 1):
        dat[arr[i - 1]] = i
 
    b = [0] * (n + 1)
    j = 0
 
    # Calculating steps required
    # for each element to reach
    # its sorted position
    for i in range(1, n + 1):
        c = 1
        j = dat[i]
        while(j != i):
            c += 1
            j = dat[j]
 
        b[i] = c
 
    # Calculate LCM of the array
    print(findlcm(b, n))
 
# Driver Code
arr = [ 5, 1, 4, 3, 2, 7, 6 ]
 
N = len(arr)
 
minimumSteps(arr, N)
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to find
// GCD of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
 
    return gcd(b, a % b);
}
 
// Function to calculate the
// LCM of array elements
static int findlcm(int []arr, int n)
{
     
    // Initialize result
    int ans = 1;
 
    for(int i = 1; i <= n; i++)
        ans = (((arr[i] * ans)) /
            (gcd(arr[i], ans)));
 
    return ans;
}
 
// Function to find minimum steps
// required to obtain sorted sequence
static void minimumSteps(int []arr, int n)
{
 
    // Inititalize dat[] array for
    // Direct Address Table.
    int i;
    int []dat = new int[n + 1];
 
    for(i = 1; i <= n; i++)
        dat[arr[i - 1]] = i;
 
    int []b = new int[n + 1];
    int j = 0, c;
 
    // Calculating steps required
    // for each element to reach
    // its sorted position
    for(i = 1; i <= n; i++)
    {
        c = 1;
        j = dat[i];
         
        while (j != i)
        {
            c++;
            j = dat[j];
        }
        b[i] = c;
    }
 
    // Calculate LCM of the array
    Console.WriteLine(findlcm(b, n));
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 5, 1, 4, 3, 2, 7, 6 };
 
    int N = arr.Length;
 
    minimumSteps(arr, N);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
6

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

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