最小化数组 A 上的旋转次数,使其等于 B
给定大小为N的数组A[]和数组B[] ,任务是最小化A上的旋转次数(左或右),使其等于B。
注意:始终可以将 A 更改为 B。
例子:
Input: A[] = {1, 2, 3, 4, 5},
B[] = {4, 5, 1, 2, 3}
Output: 2
Explanation: A[] can be changed to B[]:
On left rotating A[] 3 or,
On right rotating A[] 2 times.
Hence, the minimum number of rotations required are 2.
Input: A[] = {13, 12, 7, 18, 4, 5, 1},
B[] = {12, 7, 18, 4, 5, 1, 13}
Output: 1
Explanation: A[] can be changed to B[]:
On left rotating A[] 1 time or,
On right rotating A[] 6 times.
Hence, the minimum number of rotations required is 1.
方法:解决方案基于简单地保持旋转数组 A[] 并检查它。请按照以下步骤操作:
- 左旋转数组A[]直到它等于B[]并计算所需的旋转次数。
- 同样向右旋转数组A[]直到它等于B[]并计算旋转次数。
- 两个旋转中的最小值将是答案。
下面是上述方法的实现:
C++
// C++ code to minimize number of rotations
#include
using namespace std;
// Function to check if two arrays are equal
bool check(int A[], int B[], int N)
{
bool flag = true;
for (int i = 0; i < N; i++) {
if (A[i] != B[i]) {
flag = false;
break;
}
}
return flag;
}
// Function to left rotate an array
void Leftrotate(int A[], int N)
{
int temp = A[0];
for (int i = 0; i < N - 1; i++) {
A[i] = A[i + 1];
}
A[N - 1] = temp;
}
// Function to right rotate an array
void Rightrotate(int A[], int N)
{
int temp = A[N - 1];
for (int i = N - 1; i > 0; i--) {
A[i] = A[i - 1];
}
A[0] = temp;
}
// Function to minimize number of rotations
int minRotations(int A[], int B[], int N)
{
int C[N];
for (int i = 0; i < N; i++)
C[i] = A[i];
int a = 0, b = 0;
// Right rotate the array
// till it is equal to B
while (check(A, B, N) == false) {
Rightrotate(A, N);
a++;
}
// Left rotate the array
// till it is equal to B
while (check(C, B, N) == false) {
Leftrotate(C, N);
b++;
}
int ans = min(a, b);
return ans;
}
// Driver code
int main()
{
int A[] = { 13, 12, 7, 18, 4, 5, 1 };
int B[] = { 12, 7, 18, 4, 5, 1, 13 };
int N = sizeof(A) / sizeof(A[0]);
int ans = minRotations(A, B, N);
cout << ans;
return 0;
}
C
// C code to minimize number of rotations
#include
#include
#include
// Function to check if two arrays are equal
bool check(int A[], int B[], int N)
{
bool flag = true;
for (int i = 0; i < N; i++) {
if (A[i] != B[i]) {
flag = false;
break;
}
}
return flag;
}
// Function to left rotate an array
void Leftrotate(int A[], int N)
{
int temp = A[0];
for (int i = 0; i < N - 1; i++) {
A[i] = A[i + 1];
}
A[N - 1] = temp;
}
// Function to right rotate an array
void Rightrotate(int A[], int N)
{
int temp = A[N - 1];
for (int i = N - 1; i > 0; i--) {
A[i] = A[i - 1];
}
A[0] = temp;
}
// Function to minimize number of rotations
int minRotations(int A[], int B[], int N)
{
int ans, a = 0, b = 0;
int C[N];
for (int i = 0; i < N; i++)
C[i] = A[i];
// Right rotate the array
// till it is equal to B
while (check(A, B, N) == false) {
Rightrotate(A, N);
a++;
}
// Left rotate the array
// till it is equal to B
while (check(C, B, N) == false) {
Leftrotate(C, N);
b++;
}
ans = a <= b ? a : b;
return ans;
}
// Driver code
int main()
{
int A[] = { 13, 12, 7, 18, 4, 5, 1 };
int B[] = { 12, 7, 18, 4, 5, 1, 13 };
int N = sizeof(A) / sizeof(A[0]);
int ans = minRotations(A, B, N);
printf("%d", ans);
return 0;
}
Java
// Java code to minimize number of rotations
import java.util.*;
public class GFG
{
// Function to check if two arrays are equal
static boolean check(int A[], int B[], int N)
{
boolean flag = true;
for (int i = 0; i < N; i++) {
if (A[i] != B[i]) {
flag = false;
break;
}
}
return flag;
}
// Function to left rotate an array
static void Leftrotate(int A[], int N)
{
int temp = A[0];
for (int i = 0; i < N - 1; i++) {
A[i] = A[i + 1];
}
A[N - 1] = temp;
}
// Function to right rotate an array
static void Rightrotate(int A[], int N)
{
int temp = A[N - 1];
for (int i = N - 1; i > 0; i--) {
A[i] = A[i - 1];
}
A[0] = temp;
}
// Function to minimize number of rotations
static int minRotations(int A[], int B[], int N)
{
int C[] = new int[N];
for (int i = 0; i < N; i++)
C[i] = A[i];
int a = 0, b = 0;
// Right rotate the array
// till it is equal to B
while (check(A, B, N) == false) {
Rightrotate(A, N);
a++;
}
// Left rotate the array
// till it is equal to B
while (check(C, B, N) == false) {
Leftrotate(C, N);
b++;
}
int ans = Math.min(a, b);
return ans;
}
// Driver code
public static void main(String args[])
{
int A[] = { 13, 12, 7, 18, 4, 5, 1 };
int B[] = { 12, 7, 18, 4, 5, 1, 13 };
int N = A.length;
int ans = minRotations(A, B, N);
System.out.println(ans);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to check if two arrays are equal
def check(A, B, N):
flag = True;
for i in range(N):
if (A[i] != B[i]):
flag = False;
break;
return flag;
# Function to left rotate an array
def Leftrotate(A, N):
temp = A[0];
for i in range(N - 1):
A[i] = A[i + 1];
A[N - 1] = temp;
# Function to right rotate an array
def Rightrotate(A, N):
temp = A[N - 1];
for i in range(N - 1, 0, -1):
A[i] = A[i - 1];
A[0] = temp;
# Function to minimize number of rotations
def minRotations(A, B, N):
C = [0] * N
for i in range(N):
C[i] = A[i];
a = 0
b = 0
# Right rotate the array
# till it is equal to B
while (check(A, B, N) == False):
Rightrotate(A, N);
a += 1
# Left rotate the array
# till it is equal to B
while (check(C, B, N) == False):
Leftrotate(C, N);
b += 1
ans = min(a, b);
return ans;
# Driver code
A = [13, 12, 7, 18, 4, 5, 1];
B = [12, 7, 18, 4, 5, 1, 13];
N = len(A)
ans = minRotations(A, B, N);
print(ans);
# This code is contributed by gfgking
C#
// C# code to minimize number of rotations
using System;
public class GFG
{
// Function to check if two arrays are equal
static bool check(int[] A, int[] B, int N)
{
bool flag = true;
for (int i = 0; i < N; i++)
{
if (A[i] != B[i])
{
flag = false;
break;
}
}
return flag;
}
// Function to left rotate an array
static void Leftrotate(int[] A, int N)
{
int temp = A[0];
for (int i = 0; i < N - 1; i++)
{
A[i] = A[i + 1];
}
A[N - 1] = temp;
}
// Function to right rotate an array
static void Rightrotate(int[] A, int N)
{
int temp = A[N - 1];
for (int i = N - 1; i > 0; i--)
{
A[i] = A[i - 1];
}
A[0] = temp;
}
// Function to minimize number of rotations
static int minRotations(int[] A, int[] B, int N)
{
int[] C = new int[N];
for (int i = 0; i < N; i++)
C[i] = A[i];
int a = 0, b = 0;
// Right rotate the array
// till it is equal to B
while (check(A, B, N) == false)
{
Rightrotate(A, N);
a++;
}
// Left rotate the array
// till it is equal to B
while (check(C, B, N) == false)
{
Leftrotate(C, N);
b++;
}
int ans = Math.Min(a, b);
return ans;
}
// Driver code
public static void Main()
{
int[] A = { 13, 12, 7, 18, 4, 5, 1 };
int[] B = { 12, 7, 18, 4, 5, 1, 13 };
int N = A.Length;
int ans = minRotations(A, B, N);
Console.Write(ans);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
1
时间复杂度: O(N 2 )
辅助空间: 在)