给定两个数组arr[]和jump[] ,每个数组的长度为N ,其中jump[i]表示数组arr[] 中第i个元素可以向前移动的索引数,任务是找到最小数需要跳转,以便数组按升序排序。
笔记:
数组arr[] 的所有元素都是不同的。
跳跃时,数组元素可以重叠(即位于同一索引上)。
数组元素可以移动到超出数组大小的索引处。
例子:
Input: arr[] = {3, 1, 2}, jump[] = {1, 4, 5}
Output: 3
Explanation:
Following sequence requires minimum number of jumps to sort the array in ascending order:
Jump 1: arr[0] jumps by 1 ( = jump[0]) index to index 1.
Jump 2: arr[0] jumps by 1 ( = jump[0]) index to index 2.
Jump 3: arr[0] jumps by 1 ( = jump[0]) index to index 3.
Therefore, the minimum number of operations required is 3.
Input: arr[] = {3, 2, 1}, jump[] = {1, 1, 1}
Output: 6
Explanation:
Following sequence requires minimum number of jumps to sort the array in ascending order:
Jump 1: arr[0] jumps by 1 ( = jump[0]) index to the index 1.
Jump 2: arr[0] jumps by 1 ( = jump[0]) index to the index 2.
Jump 3: arr[0] jumps by 1 ( = jump[0]) index to the index 3.
Jump 4: arr[1] jumps by 1 ( = jump[0]) index to the index 2.
Jump 5: arr[1] jumps by 1 ( = jump[0]) index to the index 3.
Jump 6: arr[0] jumps by 1 ( = jump[0]) index to the index 4.
Therefore, the minimum number of operations required is 6.
方法:可以贪婪地解决给定的问题。请按照以下步骤解决问题:
- 初始化一个变量,比如jumps = 0 ,以存储所需的最小跳转次数。
- 初始化一个数组,比如temp[] ,其中temp[arr[i]]存储了arr[i]可以跳跃的距离
- 初始化成对向量,比如vect ,以存储数组arr[]的元素及其各自的索引,即{arr[i], i + 1}
- 对向量进行排序。
- 在索引[1, N – 1]的范围内遍历向量vect 。在vect[i].second ≤ vect[i-1].second 时更新vect[i]的值,通过将跳转距离添加到vect[i].second 。
- 增量跳跃1 。
- 打印jumps的值。
下面是上述方法的实现:
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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live