📌  相关文章
📜  按升序对给定数组进行排序所需的最小跳转次数|第 2 组

📅  最后修改于: 2022-05-13 01:56:06.723000             🧑  作者: Mango

按升序对给定数组进行排序所需的最小跳转次数|第 2 组

给定两个数组arr[]jump[] ,每个数组长度为N ,其中jump[i]表示数组arr[]中第i元素可以向前移动的索引数,任务是找到最小数所需的跳转次数,以便数组按升序排序。

  • 数组arr[]的所有元素都是不同的。
  • 跳跃时,数组元素可以重叠(即位于同一索引上)。
  • 数组元素可以移动到超出数组大小的索引

例子:

方法:这个问题的 Set-1 中提到了解决方案的幼稚方法。为了确定每个元素的跳跃次数,这里使用了 map 的帮助。对于当前元素,确定将按排序顺序出现的前一个元素,然后确定将当前元素放在该元素之后所需的跳转次数。请按照以下步骤操作:

  • 将每个元素的当前位置存储在地图中。
  • 将元素按排序顺序存储在集合中。
  • 按排序顺序查找当前元素与其前一个元素的位置差异。然后通过将差值除以当前跳跃的长度来找到所需的跳跃次数。
  • 将此计数添加到最终答案中。
  • 返回最终答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return minimum required jumps
int minJumps(
    vector arr, vector jump, int N)
{
    int temp[N];
    for (int i = 0; i < N; i++)
        temp[i] = arr[i];
    sort(temp, temp + N);
    unordered_map a;
    for (int i = 0; i < N; i++)
    {
        a[arr[i]] = i;
    }
    int ans = 0;
    int x = 1, y = 0;
    while (x < N)
    {
        if (a[temp[x]] <= a[temp[y]])
        {
            int jumps = ceil((a[temp[y]] - a[temp[x]] + 1) / jump[a[temp[x]]]);
 
            ans += jumps;
            a[temp[x]] = a[temp[x]] + jumps * jump[a[temp[x]]];
        }
        x++;
        y++;
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 3;
    vector arr = {3, 2, 1};
    vector jump = {1, 1, 1};
 
    cout << (minJumps(arr, jump, N));
    return 0;
}
 
// This code is contributed by lokeshpotta20.


Java
// Java code to implement the above approach
import java.io.*;
import java.util.*;
 
class GFG {
    // Function to return minimum required jumps
    public static int minJumps(
        int arr[], int jump[], int N)
    {
        int temp[] = new int[N];
        for (int i = 0; i < N; i++)
            temp[i] = arr[i];
        Arrays.sort(temp);
        HashMap a = new HashMap<>();
        for (int i = 0; i < N; i++) {
            a.put(arr[i], i);
        }
        int ans = 0;
        int x = 1, y = 0;
        while (x < N) {
            if (a.get(temp[x]) <= a.get(temp[y])) {
                int jumps = (int)Math.ceil(
                    (float)(a.get(temp[y])
                            - a.get(temp[x])
                            + 1)
                    / jump[a.get(temp[x])]);
                ans += jumps;
                a.put(temp[x],
                      a.get(temp[x])
                          + jumps
                                * jump[a.get(temp[x])]);
            }
            x++;
            y++;
        }
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 3;
        int arr[] = { 3, 2, 1 };
        int jump[] = { 1, 1, 1 };
 
        System.out.println(minJumps(arr, jump, N));
    }
}


Python3
# Python program for the above approach
import math as Math
 
# Function to return minimum required jumps
def minJumps(arr, jump, N):
    temp = [0] * N
    for i in range(N):
        temp[i] = arr[i]
    temp.sort()
    a = {}
    for i in range(N):
        a[arr[i]] = i
 
    ans = 0
    x = 1
    y = 0
    while x < N:
        if a[temp[x]] <= a[temp[y]]:
            jumps = Math.ceil((a[temp[y]] - a[temp[x]] + 1) / jump[a[temp[x]]])
 
            ans += jumps
            a[temp[x]] = a[temp[x]] + jumps * jump[a[temp[x]]]
        x += 1
        y += 1
    return ans
 
# Driver code
 
N = 3
arr = [3, 2, 1]
jump = [1, 1, 1]
 
print(minJumps(arr, jump, N))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to return minimum required jumps
public static int minJumps(int[] arr, int[] jump, int N)
{
    int[] temp = new int[N];
    for(int i = 0; i < N; i++)
        temp[i] = arr[i];
         
    Array.Sort(temp);
    Dictionary a = new Dictionary();
     
    for(int i = 0; i < N; i++)
    {
        a[arr[i]] = i;
    }
     
    int ans = 0;
    int x = 1, y = 0;
    while (x < N)
    {
        if (a[temp[x]] <= a[temp[y]])
        {
            int jumps = (int)Math.Ceiling(
                (float)(a[temp[y]] - a[temp[x]] + 1) /
                   jump[a[temp[x]]]);
                    
            ans += jumps;
            a[temp[x]] = a[temp[x]] + jumps * jump[a[temp[x]]];
        }
        x++;
        y++;
    }
    return ans;
}
 
// Driver code
public static void Main(string[] args)
{
    int N = 3;
    int[] arr = { 3, 2, 1 };
    int[] jump = { 1, 1, 1 };
 
    Console.Write(minJumps(arr, jump, N));
}
}
 
// This code is contributed by ukasp


Javascript


输出
6

时间复杂度: O(N * logN)
辅助空间: O(N)