给定一个包含N个元素的数组。允许在数组上执行以下任意多次移动:
- 选择任意L和R,并将L到R范围内的所有数字加1。
任务是找到以非降序对数组进行排序所需的最少数量的此类移动。
例子:
Input : arr[] = {1, 2, 3, 4}
Output : 0
The array is already sorted
Input : arr[] = {3, 2, 1}
Output : 2
Step 1: L=1 and R=2 (0-based)
Step 2: L=2 and R=2
Resultant array [3, 3, 3]
考虑一个排序的数组,增加数组的所有元素仍将导致一个排序的数组。
因此,想法是从最后一个索引开始,从右遍历数组的元素,并跟踪最小元素。如果在任何时候发现元素的顺序在增加,请从当前元素中减去右边的min元素,以计算移动数。
下面是上述方法的实现:
C++
// C++ program to find minimum range
// increments to sort an array
#include
using namespace std;
// Function to find minimum range
// increments to sort an array
int minMovesToSort(int arr[], int n)
{
int moves = 0;
int i, mn = arr[n - 1];
for (i = n - 2; i >= 0; i--) {
// If current element is found greater than
// last element
// Increment all terms in
// range i+1 to n-1
if (arr[i] > mn)
moves += arr[i] - mn;
mn = arr[i]; // Minimum in range i to n-1
}
return moves;
}
// Driver Code
int main()
{
int arr[] = { 3, 5, 2, 8, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minMovesToSort(arr, n);
return 0;
}
Java
// Java program to find minimum range
// increments to sort an array
import java.io.*;
class GFG {
// Function to find minimum range
// increments to sort an array
static int minMovesToSort(int arr[], int n)
{
int moves = 0;
int i, mn = arr[n - 1];
for (i = n - 2; i >= 0; i--) {
// If current element is found greater than
// last element
// Increment all terms in
// range i+1 to n-1
if (arr[i] > mn)
moves += arr[i] - mn;
mn = arr[i]; // Minimum in range i to n-1
}
return moves;
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 3, 5, 2, 8, 4 };
int n = arr.length;
System.out.println( minMovesToSort(arr, n));
}
}
// This code is contributed by anuj_67..
Python3
# Python3 program to find minimum range
# increments to sort an array
# Function to find minimum range
# increments to sort an array
def minMovesToSort(arr, n) :
moves = 0
mn = arr[n - 1]
for i in range(n - 1, -1, -1) :
# If current element is found
# greater than last element
# Increment all terms in
# range i+1 to n-1
if (arr[i] > mn) :
moves += arr[i] - mn
mn = arr[i] # Minimum in range i to n-1
return moves
# Driver Code
if __name__ == "__main__" :
arr = [ 3, 5, 2, 8, 4 ]
n = len(arr)
print(minMovesToSort(arr, n))
# This code is contributed by Ryuga
C#
// C# program to find minimum range
// increments to sort an array
using System;
class GFG
{
// Function to find minimum range
// increments to sort an array
static int minMovesToSort(int []arr,
int n)
{
int moves = 0;
int i, mn = arr[n - 1];
for (i = n - 2; i >= 0; i--)
{
// If current element is found
// greater than last element
// Increment all terms in
// range i+1 to n-1
if (arr[i] > mn)
moves += arr[i] - mn;
mn = arr[i]; // Minimum in range
// i to n-1
}
return moves;
}
// Driver Code
static public void Main ()
{
int []arr = { 3, 5, 2, 8, 4 };
int n = arr.Length;
Console.WriteLine(minMovesToSort(arr, n));
}
}
// This code is contributed by ajit
PHP
= 0; $i--)
{
// If current element is found
// greater than last element
// Increment all terms in
// range i+1 to n-1
if ($arr[$i] > $mn)
$moves += $arr[$i] - $mn;
$mn = $arr[$i]; // Minimum in range i to n-1
}
return $moves;
}
// Driver Code
$arr = array(3, 5, 2, 8, 4 );
$n = sizeof($arr);
echo minMovesToSort($arr, $n);
// This code is contributed
// by Akanksha Rai
?>
输出:
7
时间复杂度: O(N)