通过删除任何元素并将其插入到前面或后面来对 1 到 N 的排列进行排序
给定一个大小为N的数组arr[]具有从1到N的不同整数,任务是通过删除任何元素并将其插入到数组的前面或后面来计算以升序对数组进行排序所需的最小步数.
例子:
Input: arr[ ] = {4, 1, 3, 2}
Output: 2
Explanation:
The given array can be sorted in increasing order by following two operations:
Operation 1: Remove 3 and add it to the back of the array. {4, 1, 3, 2} -> {4, 1, 2, 3}
Operation 2: Remove 4 and add it to the back of the array. {4, 1, 2, 3} -> {1, 2, 3, 4}
Input: arr[ ] = {4, 1, 2, 5, 3}
Output: 2
方法:这个问题可以通过使用这样一个事实来解决,即为了使数组以最少的步数排序,最少数量的元素必须移动到前面或后面。此外,必须改变位置的元素最初不会按递增顺序排列。所以问题减少到找到最长的增加子数组,因为只有数组的那些元素不会被移动。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Function to find the minimum steps
// to sort the array
void findMinStepstoSort(int arr[], int N){
// Storing the positions of elements
map pos;
for(int i = 0; i < N; i++)
pos[arr[i]] = i;
// Intitalize answer
int ans = N-1;
int prev = -1;
int count = 0;
// Traversing the array
for(int i = 1; i < N + 1; i++){
// If current is greater than
// previous
if(pos[i] > prev)
count += 1;
// else if current is less than
// previous
else
count = 1;
// Updating previous
prev = pos[i];
// Updating ans
ans = min(ans, N - count);
}
cout<
Java
// JAVA program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum steps
// to sort the array
static void findMinStepstoSort(int arr[], int N){
// Storing the positions of elements
HashMap pos = new HashMap<>();
for(int i = 0; i < N; i++)
pos.put(arr[i], i);
// Intitalize answer
int ans = N - 1;
int prev = -1;
int count = 0;
// Traversing the array
for(int i = 1; i < N + 1; i++){
// If current is greater than
// previous
if(pos.get(i) > prev)
count += 1;
// else if current is less than
// previous
else
count = 1;
// Updating previous
prev = pos.get(i);
// Updating ans
ans = Math.min(ans, N - count);
}
System.out.print(ans);
}
// Driver Code
public static void main(String[] args){
int N = 5;
int arr[] = {4, 1, 2, 5, 3};
findMinStepstoSort(arr, N);
}
}
// This code is contributed by gauravrajput1
Python3
# Python program for the above approach
# Function to find the minimum steps
# to sort the array
def findMinStepstoSort(arr, N):
# Storing the positions of elements
pos = {arr[i]:i for i in range(N)}
# Intitalize answer
ans = N-1
prev = -1;count = 0
# Traversing the array
for i in range(1, N + 1):
# If current is greater than
# previous
if pos[i] > prev:
count += 1
# else if current is less than
# previous
else:
count = 1
# Updating previous
prev = pos[i]
# Updating ans
ans = min(ans, N - count)
print(ans)
# Driver Code
if __name__ == '__main__':
N = 5
arr = [4, 1, 2, 5, 3]
findMinStepstoSort(arr, N)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find the minimum steps
// to sort the array
static void findMinStepstoSort(int[] arr, int N){
// Storing the positions of elements
Dictionary pos = new
Dictionary();
for(int i = 0; i < N; i++)
pos[arr[i]] = i;
// Intitalize answer
int ans = N - 1;
int prev = -1;
int count = 0;
// Traversing the array
for(int i = 1; i < N + 1; i++){
// If current is greater than
// previous
if(pos[i] > prev)
count += 1;
// else if current is less than
// previous
else
count = 1;
// Updating previous
prev = pos[i];
// Updating ans
ans = Math.Min(ans, N - count);
}
Console.WriteLine(ans);
}
// Driver Code
public static void Main (string[] args)
{
int N = 5;
int[] arr = {4, 1, 2, 5, 3};
findMinStepstoSort(arr, N);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(N)