计算“移动到前面”移动的最小数量以对数组进行排序
给定一个大小为 n 的数组,使得数组元素在 1 到 n 的范围内。任务是计算将项目排列为 {1, 2, 3,… n} 的移动到前端操作的数量。前移操作是选择任何项目并将其放置在第一个位置。
这个问题也可以看作是一堆项目,唯一可用的移动是从堆栈中拉出一个项目并将其放在堆栈顶部。
例子 :
Input: arr[] = {3, 2, 1, 4}.
Output: 2
First, we pull out 2 and places it on top,
so the array becomes (2, 3, 1, 4). After that,
pull out 1 and becomes (1, 2, 3, 4).
Input: arr[] = {5, 7, 4, 3, 2, 6, 1}
Output: 6
We pull elements in following order
7, 6, 5, 4, 3 and 2
Input: arr[] = {4, 3, 2, 1}.
Output: 3
这个想法是从末端遍历数组。最后我们期望 n,所以我们将 expectedItem 初始化为 n。在 expectedItem 的实际位置和当前位置之间的所有项目都必须移到前面。所以我们计算当前项目和预期项目之间的项目数。一旦我们找到了expectedItem,我们通过将expectedITem 减一来寻找下一个expectedItem。
以下是最小移动次数的算法:
1. Initialize expected number at current position as n
2. Start from the last element of array.
a) If the current item is same as expected item,
decrease expected item by 1.
3. Return expected item.
下面是这种方法的实现。
C++
// C++ program to find minimum number of move-to-front
// moves to arrange items in sorted order.
#include
using namespace std;
// Calculate minimum number of moves to arrange array
// in increasing order.
int minMoves(int arr[], int n)
{
// Since we traverse array from end, expected item
// is initially n
int expectedItem = n;
// Traverse array from end
for (int i=n-1; i >= 0; i--)
{
// If current item is at its correct position,
// decrement the expectedItem (which also means
// decrement in minimum number of moves)
if (arr[i] == expectedItem)
expectedItem--;
}
return expectedItem;
}
// Driver Program
int main()
{
int arr[] = {4, 3, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << minMoves(arr, n);
return 0;
}
Java
// java program to find minimum
// number of move-to-front moves
// to arrange items in sorted order.
import java.io.*;
class GFG
{
// Calculate minimum number of moves
// to arrange array in increasing order.
static int minMoves(int arr[], int n)
{
// Since we traverse array from end,
// expected item is initially n
int expectedItem = n;
// Traverse array from end
for (int i = n - 1; i >= 0; i--)
{
// If current item is at its correct position,
// decrement the expectedItem (which also means
// decrement in minimum number of moves)
if (arr[i] == expectedItem)
expectedItem--;
}
return expectedItem;
}
// Driver Program
public static void main (String[] args)
{
int arr[] = {4, 3, 2, 1};
int n = arr.length;
System.out.println( minMoves(arr, n));
}
}
// This code is contributed by vt_m
Python3
# Python 3 program to find minimum
# number of move-to-front moves
# to arrange items in sorted order.
# Calculate minimum number of moves
# to arrange array in increasing order.
def minMoves(arr, n):
# Since we traverse array from end,
# expected item is initially n
expectedItem = n
# Traverse array from end
for i in range(n - 1, -1, -1):
# If current item is at its
# correct position, decrement
# the expectedItem (which also
# means decrement in minimum
# number of moves)
if (arr[i] == expectedItem):
expectedItem -= 1
return expectedItem
# Driver Code
arr = [4, 3, 2, 1]
n = len(arr)
print(minMoves(arr, n))
# This code is contributed 29AjayKumar
C#
// C# program to find minimum
// number of move-to-front moves
// to arrange items in sorted order.
using System;
class GFG {
// Calculate minimum number of moves
// to arrange array in increasing order.
static int minMoves(int []arr, int n)
{
// Since we traverse array from end,
// expected item is initially n
int expectedItem = n;
// Traverse array from end
for (int i = n - 1; i >= 0; i--)
{
// If current item is at its
// correct position, decrement
// the expectedItem (which also
// means decrement in minimum
// number of moves)
if (arr[i] == expectedItem)
expectedItem--;
}
return expectedItem;
}
// Driver Program
public static void Main ()
{
int []arr = {4, 3, 2, 1};
int n = arr.Length;
Console.Write( minMoves(arr, n));
}
}
// This code is contributed by nitin mittal.
PHP
= 0; $i--)
{
// If current item is at its
// correct position, decrement
// the expectedItem (which also
// means decrement in minimum
// number of moves)
if ($arr[$i] == $expectedItem)
$expectedItem--;
}
return $expectedItem;
}
// Driver Code
$arr = array(4, 3, 2, 1);
$n = count($arr);
echo minMoves($arr, $n);
// This code is contributed by anuj_67.
?>
Javascript
输出:
3