给定N个元素,找到所需的最小交换数,以便最大元素在开头,最小元素在最后,但条件是仅允许相邻元素交换。
例子:
Input: a[] = {3, 1, 5, 3, 5, 5, 2}
Output: 6
Step 1: Swap 5 with 1 to make the array as {3, 5, 1, 3, 5, 5, 2}
Step 2: Swap 5 with 3 to make the array as {5, 3, 1, 3, 5, 5, 2}
Step 3: Swap 1 with 3 at its right to make the array as {5, 3, 3, 1, 5, 5, 2}
Step 4: Swap 1 with 5 at its right to make the array as {5, 3, 3, 5, 1, 5, 2}
Step 5: Swap 1 with 5 at its right to make the array as {5, 3, 3, 5, 5, 1, 2}
Step 6: Swap 1 with 2 at its right to make the array as {5, 3, 3, 5, 5, 2, 1}
After performing 6 swapping operations 5 is at the beginning and 1 at the end
Input: a[] = {5, 6, 1, 3}
Output: 2
该方法将是找到最大元素的索引(let 1)。如果最大元素在数组中出现多次,请找到最左边最大元素的索引。同样,找到最右边的最小元素的索引(let r)。有两种情况可以解决此问题。
- 情况1:如果l
- 情况2:如果l> r:交换次数= l +(nr-2),因为已经执行了一次交换,同时将较大的元素交换到前面
C++
// CPP program to count Minimum number
// of adjacent /swaps so that the largest element
// is at beginning and the smallest element
// is at last
#include
using namespace std;
// Function that returns the minimum swaps
void solve(int a[], int n)
{
int maxx = -1, minn = a[0], l = 0, r = 0;
for (int i = 0; i < n; i++) {
// Index of leftmost largest element
if (a[i] > maxx) {
maxx = a[i];
l = i;
}
// Index of rightmost smallest element
if (a[i] <= minn) {
minn = a[i];
r = i;
}
}
if (r < l)
cout << l + (n - r - 2);
else
cout << l + (n - r - 1);
}
// Driver Code
int main()
{
int a[] = { 5, 6, 1, 3 };
int n = sizeof(a)/sizeof(a[0]);
solve(a, n);
return 0;
}
Java
// Java program to count Minimum number
// of swaps so that the largest element
// is at beginning and the
// smallest element is at last
import java.io.*;
class GFG {
// Function performing calculations
public static void minimumSwaps(int a[], int n)
{
int maxx = -1, minn = a[0], l = 0, r = 0;
for (int i = 0; i < n; i++) {
// Index of leftmost largest element
if (a[i] > maxx) {
maxx = a[i];
l = i;
}
// Index of rightmost smallest element
if (a[i] <= minn) {
minn = a[i];
r = i;
}
}
if (r < l)
System.out.println(l + (n - r - 2));
else
System.out.println(l + (n - r - 1));
}
// Driver Code
public static void main(String args[]) throws IOException
{
int a[] = { 5, 6, 1, 3 };
int n = a.length;
minimumSwaps(a, n);
}
}
Python3
# Python3 program to count
# Minimum number of adjacent
# swaps so that the largest
# element is at beginning and
# the smallest element is at last.
def minSwaps(arr):
'''Function that returns
the minimum swaps'''
n = len(arr)
maxx, minn, l, r = -1, arr[0], 0, 0
for i in range(n):
# Index of leftmost
# largest element
if arr[i] > maxx:
maxx = arr[i]
l = i
# Index of rightmost
# smallest element
if arr[i] <= minn:
minn = arr[i]
r = i
if r < l:
print(l + (n - r - 2))
else:
print(l + (n - r - 1))
# Driver code
arr = [5, 6, 1, 3]
minSwaps(arr)
# This code is contributed
# by Tuhin Patra
C#
// C# program to count Minimum
// number of swaps so that the
// largest element is at beginning
// and the smallest element is at last
using System;
class GFG
{
// Function performing calculations
public static void minimumSwaps(int []a,
int n)
{
int maxx = -1, l = 0,
minn = a[0], r = 0;
for (int i = 0; i < n; i++)
{
// Index of leftmost
// largest element
if (a[i] > maxx)
{
maxx = a[i];
l = i;
}
// Index of rightmost
// smallest element
if (a[i] <= minn)
{
minn = a[i];
r = i;
}
}
if (r < l)
Console.WriteLine(l + (n - r - 2));
else
Console.WriteLine(l + (n - r - 1));
}
// Driver Code
public static void Main()
{
int []a = { 5, 6, 1, 3 };
int n = a.Length;
// Calling function
minimumSwaps(a, n);
}
}
// This code is contributed by anuj_67.
PHP
$maxx)
{
$maxx = $a[$i];
$l = $i;
}
// Index of rightmost
// smallest element
if ($a[$i] <= $minn)
{
$minn = $a[$i];
$r = $i;
}
}
if ($r < $l)
echo $l + ($n - $r - 2);
else
echo $l + ($n - $r - 1);
}
// Driver Code
$a = array(5, 6, 1, 3);
$n = count($a);
solve($a, $n);
// This code is contributed
// by anuj_67.
?>
Javascript
2
时间复杂度: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。