Minimize cost of choosing and skipping array elements to reach end of the given array
Last Updated :
25 Sep, 2023
Given an integer X and an array cost[] consisting of N integers, the task is to find the minimum cost to reach the end of the given array starting from the first element based on the following constraints:
- Cost to visit point i is cost[i] where 1 ? i ? N.
- Cost to skip point i is min(cost[i], X) where 1 ? i ? N.
- At most 2 points can be skipped in a row.
- First and last positions cannot be skipped.
Examples:
Input: N = 6, X = 4, cost[] = {6, 3, 9, 2, 1, 3}
Output: 19
Explanation:
Follow the steps below:
Step 1: Choose element at 1. Sum = 6
Step 2: Choose element at 2. Sum = 6 + 3 = 9
Step 3: Skip element at 3. Sum = 6 + 3 + 4 = 13
Step 4: Choose element at 4. Sum = 6 + 3 + 4 + 2 = 15
Step 5: Choose element at 5. Sum = 6 + 3 + 4 + 2 + 1 = 16
Step 6: Choose element at 6. Sum = 6 + 3 + 4 + 2 + 1 + 3 = 19
Hence, the minimum cost is 19.
Input: N = 7, X = 4, cost[] = {6, 3, 9, 2, 1, 3, 4}
Output: 23
Explanation:
Follow the steps below:
Step 1: Choose element at 1. Sum = 6
Step 2: Choose element at 2. Sum = 6+3 = 9
Step 3: Skip element at 3. Sum = 6+3+4 = 13
Step 4: Choose element at 4. Sum = 6 + 3 + 4 + 2 = 15
Step 5: Choose element at 5. Sum = 6+3+4+2+1 = 16
Step 6: Choose element at 6. Sum = 6+3+4+2+1+3 = 19
Step 7: Choose element at 6. Sum = 6 + 3 + 4 + 2 + 1 + 3 + 4 = 23
Hence, the minimum cost is 23.
Naive Approach: The simplest approach is to generate all possible solutions by considering or skipping certain positions. There are two options for each element i.e., it can be skipped or can be chosen. Therefore, there can be at most 2N combinations. Check that in each combination, no more than 3 positions are skipped. Among those combinations, choose the one having the minimum cost and print the minimum cost.
Time Complexity: O(2N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming and observe that if any position i is skipped, the cost is increased by cost[i] or X but if the cost is increased by cost[i] then it’s best to choose that position as choosing the position also increases the cost by cost[i]. This implies that the minimum cost to reach position i can be found by taking the minimum among the minimum cost to reach position (i – 1), X + the minimum cost to reach position (i – 2) and 2X + the minimum cost to reach position (i – 3).
Therefore, the dp transition is as follows:
dp[i] = cost[i] + min(dp[i-1], min(2*X + dp[i-2], 2*X + dp[i-3]))
where,
dp[i] stores the minimum answer to reach position i from position 0.
Follow the below steps to solve the problem:
- Initialize an array dp[] where dp[i] will store the minimum answer to reach position i from position 0.
- Traverse the given array cost[] over the range [0, N – 1] and at each position i, update dp[i] as:
cost[i] + min(dp[i-1], min(2*X + dp[i-2], 2*X + dp[i-3]))
- After the above steps, print dp[N – 1] which stores the answer to reach position (N – 1) from position 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumCost( int * cost, int n, int x)
{
vector< int > dp(n + 2, 0);
dp[0] = cost[0];
for ( int i = 1; i < n; i++) {
if (i == 1)
dp[i] = cost[i] + dp[i - 1];
if (i == 2)
dp[i] = cost[i]
+ min(dp[i - 1],
x + dp[i - 2]);
if (i >= 3)
dp[i] = cost[i]
+ min(dp[i - 1],
min(x + dp[i - 2],
2 * x + dp[i - 3]));
}
cout << dp[n - 1];
}
int main()
{
int X = 4;
int cost[] = { 6, 3, 9, 2, 1, 3 };
int N = sizeof (cost) / sizeof (cost[0]);
minimumCost(cost, N, X);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void minimumCost( int [] cost, int n, int x)
{
int [] dp = new int [n + 2 ];
dp[ 0 ] = cost[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (i == 1 )
dp[i] = cost[i] + dp[i - 1 ];
if (i == 2 )
dp[i] = cost[i] + Math.min(dp[i - 1 ],
x + dp[i - 2 ]);
if (i >= 3 )
dp[i] = cost[i] + Math.min(dp[i - 1 ],
Math.min(x + dp[i - 2 ],
2 * x + dp[i - 3 ]));
}
System.out.println(dp[n - 1 ]);
}
public static void main(String[] args)
{
int X = 4 ;
int [] cost = { 6 , 3 , 9 , 2 , 1 , 3 };
int N = cost.length;
minimumCost(cost, N, X);
}
}
|
Python3
def minimumCost(cost, n, x):
dp = [ 0 ] * (n + 2 )
dp[ 0 ] = cost[ 0 ]
for i in range ( 1 , n):
if (i = = 1 ):
dp[i] = cost[i] + dp[i - 1 ]
if (i = = 2 ):
dp[i] = cost[i] + min (dp[i - 1 ],
x + dp[i - 2 ])
if (i > = 3 ):
dp[i] = (cost[i] +
min (dp[i - 1 ],
min (x + dp[i - 2 ],
2 * x + dp[i - 3 ])))
print (dp[n - 1 ])
if __name__ = = '__main__' :
X = 4
cost = [ 6 , 3 , 9 , 2 , 1 , 3 ]
N = len (cost)
minimumCost(cost, N, X)
|
C#
using System;
class GFG{
static void minimumCost( int [] cost, int n, int x)
{
int [] dp = new int [n + 2];
dp[0] = cost[0];
for ( int i = 1; i < n; i++)
{
if (i == 1)
dp[i] = cost[i] + dp[i - 1];
if (i == 2)
dp[i] = cost[i] + Math.Min(dp[i - 1],
x + dp[i - 2]);
if (i >= 3)
dp[i] = cost[i] + Math.Min(dp[i - 1],
Math.Min(x + dp[i - 2],
2 * x + dp[i - 3]));
}
Console.WriteLine(dp[n - 1]);
}
public static void Main()
{
int X = 4;
int [] cost = { 6, 3, 9, 2, 1, 3 };
int N = cost.Length;
minimumCost(cost, N, X);
}
}
|
Javascript
<script>
function minimumCost(cost, n, x)
{
let dp = [];
dp[0] = cost[0];
for (let i = 1; i < n; i++)
{
if (i == 1)
dp[i] = cost[i] + dp[i - 1];
if (i == 2)
dp[i] = cost[i] + Math.min(dp[i - 1],
x + dp[i - 2]);
if (i >= 3)
dp[i] = cost[i] + Math.min(dp[i - 1],
Math.min(x + dp[i - 2],
2 * x + dp[i - 3]));
}
document.write(dp[n - 1]);
}
let X = 4;
let cost = [ 6, 3, 9, 2, 1, 3 ];
let N = cost.length;
minimumCost(cost, N, X);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient approach: Space Optimization O(1)
In previous approach the current value i.e. dp[i] is depend upon the previous three values i.e. dp[i-1] , dp[i-2] and dp[i-3]. So instead of using DP array we use variables to store these computation which will optimize the space complexity from O(N) to O(1).
Implementation Steps:
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumCost( int * cost, int n, int x)
{
int prev1= cost[0] , prev2=0, prev3=0 , curr=0;
for ( int i = 1; i < n; i++) {
if (i == 1){
curr = cost[i] + prev1;
prev2 = curr;
}
if (i == 2){
curr = cost[i]+ min(prev2,x + prev1);
prev3 = curr;
}
if (i >= 3){
curr = cost[i]+ min(prev3,min(x + prev2,2 * x + prev1));
prev1 = prev2;
prev2 = prev3;
prev3 = curr;
}
}
cout << curr;
}
int main()
{
int X = 4;
int cost[] = { 6, 3, 9, 2, 1, 3 };
int N = sizeof (cost) / sizeof (cost[0]);
minimumCost(cost, N, X);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void minimumCost( int [] cost, int n, int x) {
int prev1 = cost[ 0 ], prev2 = 0 , prev3 = 0 , curr = 0 ;
for ( int i = 1 ; i < n; i++) {
if (i == 1 ) {
curr = cost[i] + prev1;
prev2 = curr;
}
if (i == 2 ) {
curr = cost[i] + Math.min(prev2, x + prev1);
prev3 = curr;
}
if (i >= 3 ) {
curr = cost[i] + Math.min(prev3, Math.min(x + prev2, 2 * x + prev1));
prev1 = prev2;
prev2 = prev3;
prev3 = curr;
}
}
System.out.println(curr);
}
public static void main(String[] args) {
int X = 4 ;
int [] cost = { 6 , 3 , 9 , 2 , 1 , 3 };
int N = cost.length;
minimumCost(cost, N, X);
}
}
|
Python3
def minimumCost(cost, n, x):
prev1 = cost[ 0 ]
prev2 = 0
prev3 = 0
curr = 0
for i in range ( 1 , n):
if i = = 1 :
curr = cost[i] + prev1
prev2 = curr
if i = = 2 :
curr = cost[i] + min (prev2, x + prev1)
prev3 = curr
if i > = 3 :
curr = cost[i] + min (prev3, min (x + prev2, 2 * x + prev1))
prev1, prev2, prev3 = prev2, prev3, curr
print (curr)
if __name__ = = '__main__' :
X = 4
cost = [ 6 , 3 , 9 , 2 , 1 , 3 ]
N = len (cost)
minimumCost(cost, N, X)
|
C#
using System;
class GFG {
static void MinimumCost( int [] cost, int n, int x) {
int prev1 = cost[0];
int prev2 = 0;
int prev3 = 0;
int curr = 0;
for ( int i = 1; i < n; i++) {
if (i == 1) {
curr = cost[i] + prev1;
prev2 = curr;
}
else if (i == 2) {
curr = cost[i] + Math.Min(prev2, x + prev1);
prev3 = curr;
}
else {
curr = cost[i] + Math.Min(prev3, Math.Min(x + prev2, 2 * x + prev1));
prev1 = prev2;
prev2 = prev3;
prev3 = curr;
}
}
Console.WriteLine(curr);
}
static void Main() {
int X = 4;
int [] cost = new int [] {6, 3, 9, 2, 1, 3};
int N = cost.Length;
MinimumCost(cost, N, X);
}
}
|
Javascript
function minimumCost(cost, n, x) {
let prev1 = cost[0], prev2 = 0, prev3 = 0, curr = 0;
for (let i = 1; i < n; i++) {
if (i === 1) {
curr = cost[i] + prev1;
prev2 = curr;
}
if (i === 2) {
curr = cost[i] + Math.min(prev2, x + prev1);
prev3 = curr;
}
if (i >= 3) {
curr = cost[i] + Math.min(prev3, Math.min(x + prev2, 2 * x + prev1));
prev1 = prev2;
prev2 = prev3;
prev3 = curr;
}
}
console.log(curr);
}
const X = 4;
const cost = [6, 3, 9, 2, 1, 3];
const N = cost.length;
minimumCost(cost, N, X);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...