Minimum Cost to Remove All Array Elements
Last Updated :
14 Jan, 2024
Given an array A[] of length N. Then your task is to find minimum cost to vanish all elements by performing any of the two types of operations given below along with cost:
- Remove first element let say X and remove next X number of elements as well, this operation costs nothing.
- Note: This operation can only be performed if (X + 1) elements exist.
- Remove first element at cost 1.
Examples:
Input: N = 7, A[] = {3, 3, 4, 5, 2, 6, 1}
Output: 0
Explanation: Initially, A[] = {3, 3, 4, 5, 2, 6, 1}
- Performing first operation: Remove A1 = 3 and also remove next 3 elements with cost 0. Updated A[] becomes: {2, 6, 1}
- Performing first operation: Remove A1 = 2 and also remove next 2 elements with cost 0. Updated A[] becomes: {}
It can be seen that A[] has no elements left. Then total cost is 0.
Input: N= 5, A[] = {1, 2, 3, 4, 5}
Output: 2
Explanation: Initially, A[] = {1, 2, 3, 4, 5}
- Performing second operation: Remove A1 = 1 with cost 1. Updated A[] is: {2, 3, 4, 5}
- Performing first operation: Remove A1 = 2 and also remove next 2 elements with cost 0. Updated A[] becomes: {5}
- Performing second operation: Remove A1 = 5 with cost 1. Updated A[] becomes: {}
Total cost to remove all the elements is 2.
Approach: Implement the idea below to solve the problem.
We have number of choices in performing operation. Thus, the problem can be solved using Dynamic Programming approach. In DP[] array following facts will take place:
- DP[i] = j represents minimum cost of removing all array elements from Nth index to ith index.
- Transition: DP[i] = min(DP[i + 1] + 1, DP[i + arr[i] + 1])
Steps were taken to solve the problem:
- Declaring DP[N + 1] array.
- Run a for loop from i = N – 1 to 0 and follow below mentioned steps under the scope of loop:
- DP[i] = DP[i + 1] + 1
- If (i + A[i] < N)
- DP[i] = min(DP[i], DP[i + A[i] + 1])
- Return DP[0].
Code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumCost( int A[], int N)
{
vector< int > dp(N + 1, 0);
for ( int i = N - 1; i >= 0; i--) {
dp[i] = dp[i + 1] + 1;
if (i + A[i] < N)
dp[i] = min(dp[i + A[i] + 1], dp[i]);
}
return dp[0];
}
int32_t main()
{
int N = 7;
int A[] = { 3, 3, 4, 5, 2, 6, 1 };
cout << minimumCost(A, N) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class MinimumCostRemoval {
static int minimumCost( int [] A, int N) {
int [] dp = new int [N + 1 ];
for ( int i = N - 1 ; i >= 0 ; i--) {
dp[i] = dp[i + 1 ] + 1 ;
if (i + A[i] < N)
dp[i] = Math.min(dp[i + A[i] + 1 ], dp[i]);
}
return dp[ 0 ];
}
public static void main(String[] args) {
int N = 7 ;
int [] A = { 3 , 3 , 4 , 5 , 2 , 6 , 1 };
System.out.println(minimumCost(A, N));
}
}
|
Python3
def minimum_cost(A, N):
dp = [ 0 ] * (N + 1 )
for i in range (N - 1 , - 1 , - 1 ):
dp[i] = dp[i + 1 ] + 1
if i + A[i] < N:
dp[i] = min (dp[i + A[i] + 1 ], dp[i])
return dp[ 0 ]
if __name__ = = "__main__" :
N = 7
A = [ 3 , 3 , 4 , 5 , 2 , 6 , 1 ]
print (minimum_cost(A, N))
|
C#
using System;
class Program
{
static int MinimumCost( int [] A, int N)
{
int [] dp = new int [N + 1];
for ( int i = N - 1; i >= 0; i--)
{
dp[i] = dp[i + 1] + 1;
if (i + A[i] < N)
dp[i] = Math.Min(dp[i + A[i] + 1], dp[i]);
}
return dp[0];
}
static void Main( string [] args)
{
int N = 7;
int [] A = { 3, 3, 4, 5, 2, 6, 1 };
Console.WriteLine(MinimumCost(A, N));
}
}
|
Javascript
function minimumCost(A, N) {
let dp = new Array(N + 1).fill(0);
for (let i = N - 1; i >= 0; i--) {
dp[i] = dp[i + 1] + 1;
if (i + A[i] < N)
dp[i] = Math.min(dp[i + A[i] + 1], dp[i]);
}
return dp[0];
}
let N = 7;
let A = [3, 3, 4, 5, 2, 6, 1];
console.log(minimumCost(A, N));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...