给定两个由 N 个正整数组成的数组 W[]和L[] ,其中 W[i]最初位于无限数轴上的位置 i。在每次向前跳跃时, W[i]可以从其当前位置j跳到位置(j + L[i])到任何空位置。任务是找到对数组进行排序所需的最小向前跳转次数。
例子:
Input: W[] = {2, 1, 4, 3}, L[] = {4, 1, 2, 4}
Output: 5
Explanation:
Initially, 2 is at position 0, 1 is at position 1, 4 is at position 2, 3 is at position 3 on the number line as: 2 1 4 3
Push number 2 to jump from its current position 0 to position 4, arrangement on the number line: _ 1 4 3 2
Push number 3 to jump from its current position 3 to position 7, arrangement on the number line: _ 1 4 _ 2 _ _ 3
Push number 4 to jump from its current position 2 to position 8, arrangement on the number line: _ 1 _ _ 2 _ _ 3 4
Therefore, the total number of jumps required is 1 + 1 + 3 = 5.
Input: W[] = {3, 1, 2}, L[] = {1, 4, 5}
Output: 3
方法:给定的问题可以通过使用贪婪方法来解决,方法是最小化数组中每个操作中没有正确定位的最小元素所需的跳转次数,并更新跳转次数。请按照以下步骤解决问题:
方法:给定的问题可以通过使用贪婪方法来解决,方法是最小化数组中每个操作中没有正确定位的最小元素所需的跳转次数,并更新跳转次数。请按照以下步骤解决问题:
- 初始化一个变量,比如ans为0来存储所需的最小跳转次数。
- 创建数组W[]的副本并将元素按排序顺序存储在数组arr[] 中。
- 初始化一个 HashMap pos来存储元素W[i]的当前位置。
- 遍历数组W[]并更新W[i]在pos 中的位置。
- 遍历[1,N-1]范围内的数组arr[ ],执行以下步骤:
- 将数组W[]中 arr[i]的位置和arr[i – 1]的位置存储在变量中,分别是curr和prev。
- 如果 curr的值大于prev ,则继续下一次迭代。
- 否则,迭代直到curr ≤ prev或curr已被占用并通过跳转增加 curr的值并将ans的值增加 1 。
- 将 arr[i]在 HashMap pos 中的位置更新为curr 。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of jumps required to sort the array
void minJumps(int w[], int l[], int n)
{
// Base Case
if (n == 1) {
cout << 0;
return;
}
// Store the required result
int ans = 0;
// Stores the current position of
// elements and their repective
// maximum jump
unordered_map pos, jump;
// Used to check if a position is
// already taken by another element
unordered_map filled;
// Stores the sorted array a[]
int a[n];
// Traverse the array w[] & update
// positions jumps array a[]
for (int i = 0; i < n; i++) {
pos[w[i]] = i;
filled[i] = true;
jump[w[i]] = l[i];
a[i] = w[i];
}
// Sort the array a[]
sort(a, a + n);
// Traverse the array a[] over
// the range [1, N-1]
for (int curr = 1;
curr < n; curr++) {
// Store the index of current
// element and its just smaller
// element in array w[]
int currElementPos
= pos[a[curr]];
int prevElementPos
= pos[a[curr - 1]];
if (currElementPos
> prevElementPos)
continue;
// Iterate until current element
// position is at most its just
// smaller element position
while (currElementPos
<= prevElementPos
|| filled[currElementPos]) {
currElementPos += jump[a[curr]];
ans++;
}
// Update the position of the
// current element
pos[a[curr]] = currElementPos;
filled[currElementPos] = true;
}
// Print the result
cout << ans;
}
// Driver Code
int main()
{
int W[] = { 2, 1, 4, 3 };
int L[] = { 4, 1, 2, 4 };
int N = sizeof(W) / sizeof(W[0]);
minJumps(W, L, N);
return 0;
}
Python3
# Python3 program for the above approach
# Function to find the minimum number
# of jumps required to sort the array
def minJumps(w, l, n):
# Base Case
if (n == 1):
print(0)
return
# Store the required result
ans = 0
# Stores the current position of
# elements and their repective
# maximum jump
pos = {}
jump = {}
# Used to check if a position is
# already taken by another element
filled = {}
# Stores the sorted array a[]
a = [0 for i in range(n)]
# Traverse the array w[] & update
# positions jumps array a[]
for i in range(n):
pos[w[i]] = i
filled[i] = True
jump[w[i]] = l[i]
a[i] = w[i]
# Sort the array a[]
a.sort()
# Traverse the array a[] over
# the range [1, N-1]
for curr in range(1, n, 1):
# Store the index of current
# element and its just smaller
# element in array w[]
currElementPos = pos[a[curr]]
prevElementPos = pos[a[curr - 1]]
if (currElementPos > prevElementPos):
continue
# Iterate until current element
# position is at most its just
# smaller element position
while (currElementPos <= prevElementPos or
(currElementPos in filled and
filled[currElementPos])):
currElementPos += jump[a[curr]]
ans += 1
# Update the position of the
# current element
pos[a[curr]] = currElementPos
filled[currElementPos] = True
# Print the result
print(ans)
# Driver Code
if __name__ == '__main__':
W = [ 2, 1, 4, 3 ]
L = [ 4, 1, 2, 4 ]
N = len(W)
minJumps(W, L, N)
# This code is contributed by SURENDRA_GANGWAR
5
时间复杂度: O(N*log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live