📜  在N点的圆环上依次访问M点所需的步骤

📅  最后修改于: 2021-05-06 08:50:11             🧑  作者: Mango

给定整数“ n”,考虑一个圆环,其中包含从“ 1”到“ n”的编号为“ n”的点,这样您就可以按照以下方式移动:

同样,给定一个整数数组(大小为“ m”),任务是查找从数组“ 1”开始到数组中每个点的步数。
例子 :

Input: n = 3, m = 3, arr[] = {2, 1, 2}
Output: 4
The sequence followed is 1->2->3->1->2

Input: n = 2, m = 1, arr[] = {2}
Output: 1
The sequence followed is 1->2

方法:让我们用cur表示当前位置,用nxt表示下一个位置。这给了我们2种情况:

  1. 如果cur小于nxt ,则可以nxt – cur步骤移至该位置
  2. 否则,您首先需要以n – cur步长到达n点,然后可以以cur步长移到nxt。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to count the steps required
int findSteps(int n, int m, int a[])
{
 
    // Start at 1
    int cur = 1;
 
    // Initialize steps
    int steps = 0;
    for (int i = 0; i < m; i++) {
 
        // If nxt is greater than cur
        if (a[i] >= cur)
            steps += (a[i] - cur);
        else
            steps += (n - cur + a[i]);
 
        // Now we are at a[i]
        cur = a[i];
    }
    return steps;
}
 
// Driver code
int main()
{
    int n = 3, m = 3;
    int a[] = { 2, 1, 2 };
    cout << findSteps(n, m, a);
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to count the steps required
static int findSteps(int n, int m,
                     int a[])
{
 
    // Start at 1
    int cur = 1;
 
    // Initialize steps
    int steps = 0;
    for (int i = 0; i < m; i++)
    {
 
        // If nxt is greater than cur
        if (a[i] >= cur)
            steps += (a[i] - cur);
        else
            steps += (n - cur + a[i]);
 
        // Now we are at a[i]
        cur = a[i];
    }
    return steps;
}
 
// Driver code
public static void main(String []args)
{
    int n = 3, m = 3;
    int a[] = { 2, 1, 2 };
    System.out.println(findSteps(n, m, a));
}
}
 
// This code is contributed by ihritik


C#
// C# implementation of the approach
using System;
 
class GFG
{
// Function to count the
// steps required
static int findSteps(int n,
                     int m, int []a)
{
 
    // Start at 1
    int cur = 1;
 
    // Initialize steps
    int steps = 0;
    for (int i = 0; i < m; i++)
    {
 
        // If nxt is greater than cur
        if (a[i] >= cur)
            steps += (a[i] - cur);
        else
            steps += (n - cur + a[i]);
 
        // Now we are at a[i]
        cur = a[i];
    }
    return steps;
}
 
// Driver code
public static void Main()
{
    int n = 3, m = 3;
    int []a = { 2, 1, 2 };
    Console.WriteLine(findSteps(n, m, a));
}
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
 
# Function to count the steps required
def findSteps(n, m, a):
 
    # Start at 1
    cur = 1
 
    # Initialize steps
    steps = 0
    for i in range(0, m):
 
        # If nxt is greater than cur
        if (a[i] >= cur):
            steps += (a[i] - cur)
        else:
            steps += (n - cur + a[i])
 
        # Now we are at a[i]
        cur = a[i]
     
    return steps
 
# Driver code
n = 3
m = 3
a = [2, 1, 2 ]
print(findSteps(n, m, a))
 
# This code is contributed by ihritik


PHP
= $cur)
            $steps += ($a[$i] - $cur);
        else
            $steps += ($n - $cur + $a[$i]);
 
        // Now we are at a[i]
        $cur = $a[$i];
    }
    return $steps;
}
 
// Driver code
$n = 3;
$m = 3;
$a = array(2, 1, 2 );
echo findSteps($n, $m, $a);
 
// This code is contributed by ihritik
?>


输出:
4

时间复杂度: O(M)

辅助空间: O(1)