📌  相关文章
📜  按递增顺序遍历圆形数组所有元素的最短路径

📅  最后修改于: 2021-10-27 06:19:32             🧑  作者: Mango

N 个不同的整数排列在一个圆上。任意两个相邻数字之间的距离为1 。任务是从最小的数字开始在这个圆上移动,然后移动到第二小的、第三小的,依此类推,直到最大的数字并打印最小旅行距离。
例子:

方法:如果我们想在索引为ij 的两个元素之间穿行,我们有两条可能的路径,长度之一是|i – j|另一个长度为n – |i – j|我们将选择距离最小的那个。
最初,我们构建了一个(element, index)对的数组,并按元素的升序对其进行排序。然后我们可以每两个连续的对,找到在这两个元素之间旅行的最短路径。
下面是上述方法的实现:

CPP
#include 
#define mod 1000000007
using namespace std;
 
// Function to return the minimum distance
int min_distance(int n, vector arr)
{
 
    // Declare a Pair[] array of size n
    vector> val(n);
 
    // Store all the (element, index) pairs
    for ( int i = 0; i < n; i++)
        val[i] = {arr[i], i};
 
    // Sort the pairs in ascending order of the value
    sort(val.begin(), val.end());
 
    int min_dist = 0;
    for (int i = 1; i < n; i++)
    {
 
        // Choose the minimum distance path
        // of the two available
        min_dist += min(abs(val[i].second - val[i - 1].second),
                    n - abs(val[i].second - val[i - 1].second));
    }
 
    return min_dist;
}
 
 
// Driver Code
int main()
{
    vector arr = {3, 6, 5, 1, 2, 4};
    int n = arr.size();
    cout << (min_distance(n, arr));
}
 
// This code is contributed by mohit kumar 29


Java
// Java implementation of the approach
import java.util.*;
 
class GFG {
 
    // Function to return the minimum distance
    static int min_distance(int n, int[] arr)
    {
 
        // Declare a Pair[] array of size n
        Pair[] val = new Pair[n];
 
        // Store all the (element, index) pairs
        for (int i = 0; i < n; i++)
            val[i] = new Pair(arr[i], i);
 
        // Sort the pairs in ascending order of the value
        Arrays.sort(val, com);
 
        int min_dist = 0;
        for (int i = 1; i < n; i++) {
 
            // Choose the minimum distance path
            // of the two available
            min_dist
                += Math.min(Math.abs(val[i].y - val[i - 1].y),
                            n - Math.abs(val[i].y - val[i - 1].y));
        }
 
        return min_dist;
    }
 
    // Comparator to be used in the
    // sorting of pairs based on the values
    static final Comparator com = new Comparator() {
        public int compare(Pair a, Pair b)
        {
            if (Integer.compare(a.x, b.x) != 0)
                return Integer.compare(a.x, b.x);
            else
                return Integer.compare(a.y, b.y);
        }
    };
 
    // Class to represent a pair
    static class Pair {
        int x, y;
        Pair(int p, int q)
        {
            x = p;
            y = q;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = new int[] { 3, 6, 5, 1, 2, 4 };
        int n = arr.length;
 
        System.out.println(min_distance(n, arr));
    }
}


Python
# Python implementation of the approach
 
# Function to return the minimum distance
def min_distance(n, arr):
     
    # Declare a Pair[] array of size n
    val = [None] * n
 
    # Store all the (element, index) pairs
    for i in range(0, n):
        val[i] = (arr[i], i)
 
    # Sort the pairs in ascending order of the value
    val.sort()
 
    min_dist = 0
    for i in range(1, n):
 
        # Choose the minimum distance path
        # of the two available
        min_dist += min(abs(val[i][1] - val[i - 1][1]),
                        n - abs(val[i][1] - val[i - 1][1]))
         
    return min_dist
 
# Driver code
if __name__ == "__main__":
     
    arr = [3, 6, 5, 1, 2, 4]
    n = len(arr)
 
    print(min_distance(n, arr))
 
# This code is contributed by Rituraj Jain


Javascript


输出:
8

时间复杂度: O(n * log(n))

辅助空间: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程