📌  相关文章
📜  按给定数组升序排序所需的最小跳转数

📅  最后修改于: 2021-05-13 23:13:25             🧑  作者: Mango

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

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

例子:

方法:可以贪婪地解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如jumps = 0 ,以存储所需的最小跳转次数。
  • 初始化一个数组,例如temp [] ,其中temp [arr [i]]存储可以被arr [i]跳过的距离
  • 初始化一个向量对,例如vect ,以存储数组arr []的元素及其相应的索引,即{arr [i],i + 1}
  • 对向量vect进行排序
  • 在索引[1,N – 1]的范围内遍历向量vect 更新VECT的值[I]VECT [I]。第二≤VECT [I-1]。第二,通过将跳跃的距离,以VECT [I]。第二
  • 增量1
  • 打印跳跃值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count minimum number
// of jumps required to sort the array
void minJumps(int arr[], int jump[], int N)
{
    // Stores minimum number of jumps
    int jumps = 0;
 
    // Stores distances of jumps
    int temp[1000];
 
    // Stores the array elements
    // with their starting indices
    vector > vect;
 
    // Push the pairs {arr[i], i+1}
    // into the vector of pairs vect
    for (int i = 0; i < N; i++) {
 
        // Update vect
        vect.push_back({ arr[i], i + 1 });
    }
 
    // Populate the array temp[]
    for (int i = 0; i < N; i++) {
 
        // Update temp[arr[i]]
        temp[arr[i]] = jump[i];
    }
 
    // Sort the vector in
    // the ascending order
    sort(vect.begin(), vect.end());
 
    for (int i = 1; i < N; i++) {
        // Jump till the previous
        // index <= current index
        while (vect[i].second <= vect[i - 1].second) {
            // Update vect[i]
            vect[i] = make_pair(vect[i].first,
                                vect[i].second
                                    + temp[vect[i].first]);
 
            // Increment the
            // number of jumps
            jumps++;
        }
    }
 
    // Print the minimum number
    // of jumps required
    cout << jumps << endl;
}
 
// Driver Code
int main()
{
    // Input
    int arr[] = { 3, 2, 1 };
    int jump[] = { 1, 1, 1 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minJumps(arr, jump, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to count minimum number
// of jumps required to sort the array
static void minJumps(int arr[], int jump[], int N)
{
     
    // Stores minimum number of jumps
    int jumps = 0;
 
    // Stores distances of jumps
    int temp[] = new int[1000];
 
    // Stores the array elements
    // with their starting indices
    int vect[][] = new int[N][2];
 
    // Push the pairs {arr[i], i+1}
    // into the vector of pairs vect
    for(int i = 0; i < N; i++)
    {
         
        // Update vect
        vect[i][0] = arr[i];
        vect[i][1] = i + 1;
    }
 
    // Populate the array temp[]
    for(int i = 0; i < N; i++)
    {
         
        // Update temp[arr[i]]
        temp[arr[i]] = jump[i];
    }
 
    // Sort the vector in
    // the ascending order
    Arrays.sort(vect, (a, b) -> {
        if (a[0] != b[0])
            return a[0] - b[0];
             
        return a[1] - b[1];
    });
 
    for(int i = 1; i < N; i++)
    {
         
        // Jump till the previous
        // index <= current index
        while (vect[i][1] <= vect[i - 1][1])
        {
             
            // Update vect[i]
            vect[i][0] = vect[i][0];
            vect[i][1] = vect[i][1] + temp[vect[i][0]];
 
            // Increment the
            // number of jumps
            jumps++;
        }
    }
 
    // Print the minimum number
    // of jumps required
    System.out.println(jumps);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int arr[] = { 3, 2, 1 };
    int jump[] = { 1, 1, 1 };
 
    // Size of the array
    int N = arr.length;
 
    minJumps(arr, jump, N);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to count minimum number
# of jumps required to sort the array
def minJumps(arr, jump, N):
     
    # Stores minimum number of jumps
    jumps = 0
 
    # Stores distances of jumps
    temp = [0 for i in range(1000)]
 
    # Stores the array elements
    # with their starting indices
    vect = []
     
    # Push the pairs {arr[i], i+1}
    # into the vector of pairs vect
    for i in range(N):
         
        # Update vect
        vect.append([arr[i], i + 1])
 
    # Populate the array temp[]
    for i in range(N):
         
        # Update temp[arr[i]]
        temp[arr[i]] = jump[i]
 
    # Sort the vector in
    # the ascending order
    vect.sort(reverse = False)
 
    for i in range(N):
         
        # Jump till the previous
        # index <= current index
        while (vect[i][1] <= vect[i - 1][1]):
             
            # Update vect[i]
            vect[i] = [vect[i][0], vect[i][1] +
                  temp[vect[i][0]]]
 
            # Increment the
            # number of jumps
            jumps += 1
 
    # Print the minimum number
    # of jumps required
    print(jumps)
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    arr =  [3, 2, 1]
    jump = [1, 1, 1]
 
    # Size of the array
    N = len(arr)
 
    minJumps(arr, jump, N)
 
# This code is contributed by SURENDRA_GANGWAR


输出:
6

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