数组之间按排序顺序完成遍历所需的移动次数
给定两个排序数组,大小为N的X[]和大小为M的Y[]具有唯一值。任务是计算数组之间的移动总数,以按升序遍历两个数组中的所有元素,如果最初,遍历从X[]数组开始。
例子:
Input: X[] = {1}, Y[] = {2, 3, 4}
Output: 1
Explanation: Only 1 move is required after traversing the X array and then move to the 0 index of the Y array and traverse its rest of the values.
Input: X[] = {1, 3, 4}, Y[] = {2, 5, 6}
Output: 3
方法:给定的问题可以使用两指针技术来解决。请按照以下步骤解决问题:
- 初始化两个指针,比如i为0 , j为0 ,分别指向X[]和Y[]数组。
- 将另一个变量total_moves初始化为0以存储所需的移动次数。
- 由于遍历总是从X[]数组开始,所以首先比较当前索引处的值。出现两种情况:
- 如果当前存在于X[]数组中:
- 如果X[i] < Y[j] ,只需增加索引i 。
- 如果X[i] > Y[j] ,增加total_moves和索引j 。
- 如果当前存在于Y[]数组中:
- 如果Y[j] < X[i] ,则增加索引j 。
- 如果Y[j] > X[i] ,增加total_moves和索引i 。
- 重复上述步骤,直到任何一个数组遍历完成。
- 一旦上述循环完成, total_moves将在以下两个条件下递增:
- 如果遍历在X数组和j < M处完成,则将total_moves增加1 。
- 如果遍历在Y数组处结束并且i < N ,则将total_moves增加1 。
- 打印total_moves的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of moves
// required between the arrays to complete
// the traversal in sorted order
int numberofMoves(int X[], int Y[], int n, int m)
{
// Variable to check if present
// at X array or not
bool present_at_X = true;
// Store total number of moves
int total_moves = 0;
// Initialize i and j pointing to X and
// Y array respectively
int i = 0, j = 0;
// Loop until one array is
// completely traversed
while (i < n && j < m) {
// If currently present at X array,
// and X[i]
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the number of moves
// required between the arrays to complete
// the traversal in sorted order
static int numberofMoves(int[] X, int Y[], int n, int m)
{
// Variable to check if present
// at X array or not
boolean present_at_X = true;
// Store total number of moves
int total_moves = 0;
// Initialize i and j pointing to X and
// Y array respectively
int i = 0, j = 0;
// Loop until one array is
// completely traversed
while (i < n && j < m) {
// If currently present at X array,
// and X[i]
Python3
# Python3 program for the above approach
# Function to find the number of moves
# required between the arrays to complete
# the traversal in sorted order
def numberofMoves(X, Y, n, m):
# Variable to check if present
# at X array or not
present_at_X = True
# Store total number of moves
total_moves = 0
# Initialize i and j pointing to X and
# Y array respectively
i, j = 0, 0
# Loop until one array is
# completely traversed
while (i < n and j < m):
# If currently present at X array,
# and X[i]
C#
// C# program for the above approach
using System;
public class GFG{
// Function to find the number of moves
// required between the arrays to complete
// the traversal in sorted order
static int numberofMoves(int[] X, int[] Y, int n, int m)
{
// Variable to check if present
// at X array or not
bool present_at_X = true;
// Store total number of moves
int total_moves = 0;
// Initialize i and j pointing to X and
// Y array respectively
int i = 0, j = 0;
// Loop until one array is
// completely traversed
while (i < n && j < m) {
// If currently present at X array,
// and X[i]
Javascript
输出
3
时间复杂度: O(N+M)
辅助空间: O(1)