N个自然数的给定排列之间的绝对差
给定从1到N的前N个自然数的两个排列数组arr[]和brr[] ,任务是找出它们按字典顺序排列的位置之间的绝对差。
例子:
Input: arr[] = {1, 3, 2}, brr[] = {3, 1, 2}
Output: 3
Explanation:
There are 6 possible permutations of the first N(= 3) natural numbers. They are {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}. The position of arr[] is 2 and the position of brr[] is 5. Therefore, the answer is |2 – 5| = 3.
Input: arr[] = {1, 3, 2, 4}, brr[] = {1, 3, 2, 4}
Output: 0
方法:给定的问题可以通过使用函数生成前N个自然数的下一个排列来解决 下一个排列 STL。这个想法是将arr[]视为基本排列并执行next_permutation()直到arr[]不等于brr[] 。在这一步之后,将arr[]转换为brr[]所需的步骤计数是它们按字典顺序的位置之间的绝对差的结果值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the distance between
// two permutations array arr[] and brr[]
// in lexicographical order
int permutationDiff(vector arr,
vector brr)
{
// Check if both permutations are
// already equal or not
if (arr == brr) {
return 0;
}
// Stores the distance between the
// two permutations arr[] and brr[]
int count = 0;
while (arr != brr) {
// Increment the count
count++;
// call next_permutation
next_permutation(brr.begin(),
brr.end());
}
// Return the total count
return count;
}
// Driver Code
int main()
{
vector arr = { 1, 3, 2 };
vector brr = { 3, 1, 2 };
cout << permutationDiff(arr, brr);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Utility function to find next permutation
public static void next_permutation(int nums[])
{
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
// Utility function to swap two elements in array
public static void swap(int nums[], int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
// Utility function to reverse the array in a range
public static void reverse(int nums[], int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
// Function to find the distance between
// two permutations array arr[] and brr[]
// in lexicographical order
public static int permutationDiff(int arr[], int brr[])
{
// Check if both permutations are
// already equal or not
if (Arrays.equals(arr, brr)) {
return 0;
}
// Stores the distance between the
// two permutations arr[] and brr[]
int count = 0, flag = 0;
while(!Arrays.equals(arr, brr))
{
// Increment the count
count++;
// call next_permutation
next_permutation(brr);
}
// Return the total count
return count;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 1, 3, 2 };
int []brr = { 3, 1, 2 };
System.out.println(permutationDiff(arr, brr));
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python program for the above approach
# Function for next permutation
def next_permutation(arr):
# find the length of the array
n = len(arr)
# start from the right most digit and find the first
# digit that is smaller than the digit next to it.
k = n - 2
while k >= 0:
if arr[k] < arr[k + 1]:
break
k -= 1
# reverse the list if the digit that is smaller than the
# digit next to it is not found.
if k < 0:
arr = arr[::-1]
else:
# find the first greatest element than arr[k] from the
# end of the list
for l in range(n - 1, k, -1):
if arr[l] > arr[k]:
break
# swap the elements at arr[k] and arr[l
arr[l], arr[k] = arr[k], arr[l]
# reverse the list from k + 1 to the end to find the
# most nearest greater number to the given input number
arr[k + 1:] = reversed(arr[k + 1:])
return arr
# Function to find the distance between
# two permutations array arr[] and brr[]
# in lexicographical order
def permutationDiff(arr,
brr):
# Check if both permutations are
# already equal or not
if (arr == brr):
return 0
# Stores the distance between the
# two permutations arr[] and brr[]
count = 0
while (arr != brr):
# Increment the count
count = count+1
# call next_permutation
brr = next_permutation(brr)
# Return the total count
return count
arr = [1, 3, 2]
brr = [3, 1, 2]
print(permutationDiff(arr, brr))
# This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG
{
// Utility function to find next permutation
static void next_permutation(int []nums)
{
int mark = -1;
for (int i = nums.Length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.Length - 1);
return;
}
int idx = nums.Length-1;
for (int i = nums.Length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.Length - 1);
}
// Utility function to swap two elements in array
static void swap(int []nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
// Utility function to reverse the array in a range
public static void reverse(int []nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
// Function to find the distance between
// two permutations array arr[] and brr[]
// in lexicographical order
public static int permutationDiff(int []arr, int []brr)
{
// Check if both permutations are
// already equal or not
if (arr.SequenceEqual(brr)) {
return 0;
}
// Stores the distance between the
// two permutations arr[] and brr[]
int count = 0, flag = 0;
while(!arr.SequenceEqual(brr))
{
// Increment the count
count++;
// call next_permutation
next_permutation(brr);
}
// Return the total count
return count;
}
// Driver Code
public static void Main()
{
int []arr = { 1, 3, 2 };
int []brr = { 3, 1, 2 };
Console.Write(permutationDiff(arr, brr));
}
}
// This code is contributed by Samim Hossain Mondal
Javascript
输出:
3
时间复杂度: O(N 2 )
辅助空间: O(1)