给定一个整数数组,我们需要以最少的步骤对这个数组进行排序,其中在一个步骤中,我们可以将任何数组元素从其位置插入到任何其他位置。
例子 :
Input : arr[] = [2, 3, 5, 1, 4, 7, 6]
Output : 3
We can sort above array in 3 insertion
steps as shown below,
1 before array value 2
4 before array value 5
6 before array value 7
Input : arr[] = {4, 6, 5, 1}
Output : 2
我们可以使用动态规划来解决这个问题。要观察的主要事情是移动元素不会改变除被移动元素之外的元素的相对顺序。现在考虑最长递增子序列(LIS),其中相等的元素也被作为递增序列的一部分,现在如果保持这个递增序列的元素不变并移动所有其他元素,那么它将花费最少的步数,因为我们采取了不需要移动的最长子序列。最后,答案将是数组的大小减去最长递增子序列的大小。
由于 LIS 问题可以使用动态规划在 O(N^2) 和 O(N) 额外空间中解决。
下面是上述想法的实现。
C++
// C++ program to get minimum number of insertion
// steps to sort an array
#include
using namespace std;
// method returns min steps of insertion we need
// to perform to sort array 'arr'
int minInsertionStepToSortArray(int arr[], int N)
{
// lis[i] is going to store length of lis
// that ends with i.
int lis[N];
/* Initialize lis values for all indexes */
for (int i = 0; i < N; i++)
lis[i] = 1;
/* Compute optimized lis values in bottom up manner */
for (int i = 1; i < N; i++)
for (int j = 0; j < i; j++)
if (arr[i] >= arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
/* The overall LIS must end with of the array
elements. Pick maximum of all lis values */
int max = 0;
for (int i = 0; i < N; i++)
if (max < lis[i])
max = lis[i];
// return size of array minus length of LIS
// as final result
return (N - max);
}
// Driver code to test above methods
int main()
{
int arr[] = {2, 3, 5, 1, 4, 7, 6};
int N = sizeof(arr) / sizeof(arr[0]);
cout << minInsertionStepToSortArray(arr, N);
return 0;
}
Java
// Java program to get minimum number of insertion
// steps to sort an array
class Main
{
// method returns min steps of insertion we need
// to perform to sort array 'arr'
static int minInsertionStepToSortArray(int arr[], int N)
{
// lis[i] is going to store length of lis
// that ends with i.
int[] lis = new int[N];
/* Initialize lis values for all indexes */
for (int i = 0; i < N; i++)
lis[i] = 1;
/* Compute optimized lis values in bottom up manner */
for (int i = 1; i < N; i++)
for (int j = 0; j < i; j++)
if (arr[i] >= arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
/* The overall LIS must end with of the array
elements. Pick maximum of all lis values */
int max = 0;
for (int i = 0; i < N; i++)
if (max < lis[i])
max = lis[i];
// return size of array minus length of LIS
// as final result
return (N - max);
}
// Driver code to test above methods
public static void main (String[] args)
{
int arr[] = {2, 3, 5, 1, 4, 7, 6};
int N = arr.length;
System.out.println(minInsertionStepToSortArray(arr, N));
}
}
/* This code is contributed by Harsh Agarwal */
Python 3
# Python3 program to get minimum number
# of insertion steps to sort an array
# method returns min steps of insertion
# we need to perform to sort array 'arr'
def minInsertionStepToSortArray(arr, N):
# lis[i] is going to store length
# of lis that ends with i.
lis = [0] * N
# Initialize lis values for all indexes
for i in range(N):
lis[i] = 1
# Compute optimized lis values in
# bottom up manner
for i in range(1, N):
for j in range(i):
if (arr[i] >= arr[j] and
lis[i] < lis[j] + 1):
lis[i] = lis[j] + 1
# The overall LIS must end with of the array
# elements. Pick maximum of all lis values
max = 0
for i in range(N):
if (max < lis[i]):
max = lis[i]
# return size of array minus length
# of LIS as final result
return (N - max)
# Driver Code
if __name__ == "__main__":
arr = [2, 3, 5, 1, 4, 7, 6]
N = len(arr)
print (minInsertionStepToSortArray(arr, N))
# This code is contributed by ita_c
C#
// C# program to get minimum number of
// insertion steps to sort an array
using System;
class GfG {
// method returns min steps of insertion
// we need to perform to sort array 'arr'
static int minInsertionStepToSortArray(
int []arr, int N)
{
// lis[i] is going to store length
// of lis that ends with i.
int[] lis = new int[N];
/* Initialize lis values for all
indexes */
for (int i = 0; i < N; i++)
lis[i] = 1;
/* Compute optimized lis values in
bottom up manner */
for (int i = 1; i < N; i++)
for (int j = 0; j < i; j++)
if (arr[i] >= arr[j] &&
lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
/* The overall LIS must end with of
the array elements. Pick maximum
of all lis values */
int max = 0;
for (int i = 0; i < N; i++)
if (max < lis[i])
max = lis[i];
// return size of array minus length
// of LIS as final result
return (N - max);
}
// Driver code to test above methods
public static void Main (String[] args)
{
int []arr = {2, 3, 5, 1, 4, 7, 6};
int N = arr.Length;
Console.Write(
minInsertionStepToSortArray(arr, N));
}
}
// This code is contributed by parashar.
PHP
= $arr[$j] &&
$lis[$i] < $lis[$j] + 1)
$lis[$i] = $lis[$j] + 1;
/* The overall LIS must end with
of the array elements. Pick
maximum of all lis values */
$max = 0;
for ($i = 0; $i < $N; $i++)
if ($max < $lis[$i])
$max = $lis[$i];
// return size of array minus
// length of LIS as final result
return ($N - $max);
}
// Driver code
$arr = array(2, 3, 5, 1, 4, 7, 6);
$N = sizeof($arr) / sizeof($arr[0]);
echo minInsertionStepToSortArray($arr, $N);
// This code is contributed by nitin mittal
?>
Javascript
输出 :
3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。