根据给定步骤将所有数组元素减少到 1 所需的最小步骤
给定一个大小为N的数组arr[] 。任务是找到将所有数组元素减少到 1 所需的最小步骤。在每个步骤中,执行以下给定操作:
- 选择任何起始索引,比如i ,然后跳转到第 ( arr[i] + i )索引,将第 i和第 ( arr [i] + i ) 索引减 1,按照此过程直到到达数组
- 如果一个元素已经减少到 1,它不能再减少,它保持不变。
例子:
Input: arr[] = {4, 2, 3, 2, 2, 2, 1, 2}, N = 8
Output: 5
Explanation: Series of operations can be performed in the following way:
- {4, 2, 3, 2, 2, 2, 1, 2}, decrement values by 1, arr[] = {4, 2, 2, 2, 2, 1, 1, 1}
- {4, 2, 2, 2, 2, 1, 1, 1}, decrement values by 1, arr[] = {3, 2, 2, 2, 1, 1, 1, 1}
- {3, 2, 2, 2, 1, 1, 1, 1}, decrement values by 1, arr[] = {2, 2, 2, 1, 1, 1, 1, 1}
- {2, 2, 2, 1, 1, 1, 1, 1}, decrement values by 1, arr[] = {1, 2, 1, 1, 1, 1, 1, 1}
- {1, 2, 1, 1, 1, 1, 1, 1}, decrement values by 1, arr[] = {1, 1, 1, 1, 1, 1, 1, 1}
So, total steps required = 5
Input: arr[] = {1, 3, 1, 2, 2}, N = 5
Output: 2
方法:给定的问题可以通过将问题分为 4 个部分来解决:-(0 到 i-1) |我| (i + 1) | (i + 2 到 n – 1)。请按照以下步骤解决问题:
- 拿一个向量说v ,它表示一个元素由于访问之前的元素而减少了多少次。
- v 中的每个元素,即v[i]表示由于访问从0到 ( i-1 ) 的元素而导致的arr[i]中的递减计数。
- 遍历数组arr[] ,并取一个变量k来存储由于元素arr[i]在使0到 ( i-1 ) 元素等于1之后必须添加到答案中的遍数。
- 在循环内部,运行另一个循环(即第二个循环)来计算当前arr[i]元素对 ( i+2 ) 到N个元素的影响(访问第 i 个元素后减少)。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum steps
// required to reduce all array
// elements to 1
int minSteps(int arr[], int N)
{
// Variable to store the answer
int steps = 0;
// Vector with all elements initialized
// with 0
vector v(N + 1, 0);
// Traverse the array
for (int i = 0; i < N; ++i) {
// Variable to store the numbers of
// passes that have to add in answer
// due to element arr[i] after making
// 0 to (i-1) elements equal to 1
int k = max(0ll, arr[i] - 1 - v[i]);
// Increment steps by K
steps += k;
// Update element in v
v[i] += k;
// Loop to compute how much current element
// effect the (i+2) to N elements
for (int j = i + 2; j <= min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1] += v[i] - arr[i] + 1;
}
// Return steps which is the answer
return steps;
}
// Driver Code
int main()
{
int arr[] = { 4, 2, 3, 2, 2, 2, 1, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << minSteps(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
class GFG
{
// Function to find minimum steps
// required to reduce all array
// elements to 1
static int minSteps(int arr[], int N)
{
// Variable to store the answer
int steps = 0;
// Vector with all elements initialized
// with 0
int v[] = new int[N + 1];
// Traverse the array
for (int i = 0; i < N; ++i)
{
// Variable to store the numbers of
// passes that have to add in answer
// due to element arr[i] after making
// 0 to (i-1) elements equal to 1
int k = Math.max(0, arr[i] - 1 - v[i]);
// Increment steps by K
steps += k;
// Update element in v
v[i] += k;
// Loop to compute how much current element
// effect the (i+2) to N elements
for (int j = i + 2;
j <= Math.min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1] += v[i] - arr[i] + 1;
}
// Return steps which is the answer
return steps;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 2, 3, 2, 2, 2, 1, 2 };
int N = arr.length;
System.out.println(minSteps(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function to find minimum steps
# required to reduce all array
# elements to 1
def minSteps(arr, N):
# Variable to store the answer
steps = 0
# Vector with all elements initialized
# with 0
v = [0] * (N + 1)
# Traverse the array
for i in range(N):
# Variable to store the numbers of
# passes that have to add in answer
# due to element arr[i] after making
# 0 to (i-1) elements equal to 1
k = max(0, arr[i] - 1 - v[i])
# Increment steps by K
steps += k
# Update element in v
v[i] += k
# Loop to compute how much current element
# effect the (i+2) to N elements
for j in range(i + 2, min(i + arr[i], N) + 1):
v[j] += 1
v[i + 1] += v[i] - arr[i] + 1
# Return steps which is the answer
return steps
# Driver Code
arr = [4, 2, 3, 2, 2, 2, 1, 2]
N = len(arr)
print(minSteps(arr, N))
# This code is contributed by gfgking.
C#
// C# code for the above approach
using System;
class GFG
{
// Function to find minimum steps
// required to reduce all array
// elements to 1
static int minSteps(int[] arr, int N)
{
// Variable to store the answer
int steps = 0;
// Vector with all elements initialized
// with 0
int[] v = new int[N + 1];
// Traverse the array
for (int i = 0; i < N; ++i)
{
// Variable to store the numbers of
// passes that have to add in answer
// due to element arr[i] after making
// 0 to (i-1) elements equal to 1
int k = Math.Max(0, arr[i] - 1 - v[i]);
// Increment steps by K
steps += k;
// Update element in v
v[i] += k;
// Loop to compute how much current element
// effect the (i+2) to N elements
for (int j = i + 2;
j <= Math.Min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1] += v[i] - arr[i] + 1;
}
// Return steps which is the answer
return steps;
}
// Driver Code
public static void Main()
{
int[] arr = { 4, 2, 3, 2, 2, 2, 1, 2 };
int N = arr.Length;
Console.Write(minSteps(arr, N));
}
}
// This code is contributed by gfgking
Javascript
输出:
5
时间复杂度: O(N 2 )
辅助空间: O(N)