Minimum cost to build N blocks from one block
Given a number N, the task is to build N blocks from 1 block by performing following operation:
- Double the number of blocks present in the container and cost for this operation is X.
- Increase the number of blocks present in the container by one and cost for this operation is Y.
- Decrease the number of blocks present in the container by one and cost for this operation is Z.
Examples:
Input: N = 5, X = 2, Y = 1, Z = 3
Output: 4
Explanation:
In the first operation just increase the number of blocks by one, cost = 1 and block count = 2
In the second operation double the blocks, cost = 3 and block count = 4
In the third operation again increase the number_of_blocks by one, cost = 4 and block count = 5
So minimum cost = 4
Input: N = 7, X = 1, Y = 7, Z = 2
Output: 5
Approach:
- Let f(i) denotes minimum cost to build i blocks. So f(i) = min(f(2*i)+X, f(i-1)+Y, f(i+1)+Z) Here current value depends on its next value as well as its previous value but you know that in dynamic programming current value depends only on its previous value. So try to convert its next value to its previous value. We can write f(2*i) as f(i/2) because when we have already built i/2 blocks then we can either double the number of current blocks or increase number of blocks by 1 or decrease number of blocks by 1. Here no need to calculate f(2*i).
Now
f(i) = min(f(i/2)+X, f(i-1)+Y, f(i+1)+Z)
- If i is even then (i+1) must be odd. We can build (i) blocks only by performing the increment operation and the double operation, so you don’t need to perform the decrement operation why? This is because (i+1) is odd number.So if you build (i+1) blocks by performing increment operation then it is confirmed that we have already built i blocks which means we have the minimum cost of building i blocks.If we perform any other operation it must increase your optimal cost which is irrelevant. So recurrence relation when i is even:
f(i)=min(f(i/2)+X, f(i-1)+Y)
- If i is odd then (i+1) must be even. We can build (i+1) blocks only by performing double operation or decrement operation why not increment operation? This is because if you have already built i blocks then no need to build (i+1) blocks because it will add more cost. So we can calculate f(i+1)=f((i+1)/2)+ X. So recurrence relation when i is odd:
f(i)=min(f(i-1)+Y, f( (i+1)/2)+X+Z)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost( int n, int x, int y, int z)
{
int dp[n + 1] = { 0 };
dp[0] = dp[1] = 0;
for ( int i = 2; i <= n; i++) {
if (i % 2 == 1) {
dp[i] = min(
dp[(i + 1) / 2] + x + z,
dp[i - 1] + y);
}
else {
dp[i] = min(dp[i / 2] + x,
dp[i - 1] + y);
}
}
return dp[n];
}
int main()
{
int n = 5, x = 2, y = 1, z = 3;
cout << minCost(n, x, y, z) << endl;
return 0;
}
|
Java
class GFG
{
static int minCost( int n, int x, int y, int z)
{
int dp[] = new int [n + 1 ];
dp[ 0 ] = dp[ 1 ] = 0 ;
for ( int i = 2 ; i <= n; i++)
{
if (i % 2 == 1 )
{
dp[i] = Math.min(
dp[(i + 1 ) / 2 ] + x + z,
dp[i - 1 ] + y);
}
else
{
dp[i] = Math.min(dp[i / 2 ] + x,
dp[i - 1 ] + y);
}
}
return dp[n];
}
public static void main(String[] args)
{
int n = 5 , x = 2 , y = 1 , z = 3 ;
System.out.print(minCost(n, x, y, z) + "\n" );
}
}
|
Python
def minCost(n, x, y, z):
dp = [ 0 ] * (n + 1 )
dp[ 0 ] = dp[ 1 ] = 0
for i in range ( 2 , n + 1 ):
if (i % 2 = = 1 ):
dp[i] = min (dp[(i + 1 ) / / 2 ] + x + z, dp[i - 1 ] + y)
else :
dp[i] = min (dp[i / / 2 ] + x, dp[i - 1 ] + y)
return dp[n]
n = 5
x = 2
y = 1
z = 3
print (minCost(n, x, y, z))
|
C#
using System;
class GFG
{
static int minCost( int n, int x, int y, int z)
{
int []dp = new int [n + 1];
dp[0] = dp[1] = 0;
for ( int i = 2; i <= n; i++)
{
if (i % 2 == 1)
{
dp[i] = Math.Min(
dp[(i + 1) / 2] + x + z,
dp[i - 1] + y);
}
else
{
dp[i] = Math.Min(dp[i / 2] + x,
dp[i - 1] + y);
}
}
return dp[n];
}
public static void Main(String[] args)
{
int n = 5, x = 2, y = 1, z = 3;
Console.Write(minCost(n, x, y, z) + "\n" );
}
}
|
Javascript
function minCost(n, x, y, z) {
let dp = new Array(n + 1).fill(0);
dp[0] = dp[1] = 0;
for (let i = 2; i <= n; i++) {
if (i % 2 === 1) {
dp[i] = Math.min(dp[(i + 1) / 2] + x + z, dp[i - 1] + y);
}
else {
dp[i] = Math.min(dp[i / 2] + x, dp[i - 1] + y);
}
}
return dp[n];
}
let n = 5, x = 2, y = 1, z = 3;
console.log(minCost(n, x, y, z));
|
Output :
4
Time complexity : O(N)
Space complexity : O(N) as a dp array of size N+1 is used.
Efficient approach : Space optimization
The previous implementation uses an array of size n+1 to store the minimum cost to build each number of blocks up to n. but we only need to keep track of the previous two minimum costs in order to compute the current minimum cost. Therefore, we can use just two variables to solve the problem.
Implementation steps:
- Initialize two variables prev1 and prev2 to store the minimum costs for the previous two blocks, both set to 0 initially.
- Loop from i = 2 to n.
- Inside the loop, check if i is odd or even.
- If i is odd, set the current cost cur to the minimum of prev1 + x + z and prev2 + y.
- If i is even, set cur to the minimum of prev1 + x and prev2 + y.
- Update prev1 to be equal to prev2.
- Update prev2 to be equal to cur.
- Return prev2 after the loop has completed. This will be the minimum cost to build n blocks.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost( int n, int x, int y, int z)
{
int prev1 = 0, prev2 = 0;
for ( int i = 2; i <= n; i++) {
int cur;
if (i % 2 == 1) {
cur = min(prev1 + x + z, prev2 + y);
}
else {
cur = min(prev1 + x, prev2 + y);
}
prev1 = prev2;
prev2 = cur;
}
return prev2;
}
int main()
{
int n = 5, x = 2, y = 1, z = 3;
cout << minCost(n, x, y, z) << endl;
return 0;
}
|
Java
import java.util.*;
public class MinCostCalculator {
/**
* Calculates the minimum cost to reach the nth step using given costs for different operations.
*
* @param n Number of steps
* @param x Cost of operation 1
* @param y Cost of operation 2
* @param z Cost of operation 3
* @return Minimum cost to reach the nth step
*/
public static int minCost( int n, int x, int y, int z) {
int prev1 = 0 , prev2 = 0 ;
for ( int i = 2 ; i <= n; i++) {
int cur;
if (i % 2 == 1 ) {
cur = Math.min(prev1 + x + z, prev2 + y);
} else {
cur = Math.min(prev1 + x, prev2 + y);
}
prev1 = prev2;
prev2 = cur;
}
return prev2;
}
public static void main(String[] args) {
int n = 5 , x = 2 , y = 1 , z = 3 ;
System.out.println(minCost(n, x, y, z));
}
}
|
Python3
def minCost(n: int , x: int , y: int , z: int ) - > int :
prev1 = 0
prev2 = 0
for i in range ( 2 , n + 1 ):
if i % 2 = = 1 :
cur = min (prev1 + x + z, prev2 + y)
else :
cur = min (prev1 + x, prev2 + y)
prev1 = prev2
prev2 = cur
return prev2
n = 5
x = 2
y = 1
z = 3
print (minCost(n, x, y, z))
|
C#
using System;
class Program
{
static int MinCost( int n, int x, int y, int z)
{
int prev1 = 0, prev2 = 0;
for ( int i = 2; i <= n; i++)
{
int cur;
if (i % 2 == 1)
{
cur = Math.Min(prev1 + x + z, prev2 + y);
}
else
{
cur = Math.Min(prev1 + x, prev2 + y);
}
prev1 = prev2;
prev2 = cur;
}
return prev2;
}
static void Main( string [] args)
{
int n = 5, x = 2, y = 1, z = 3;
Console.WriteLine(MinCost(n, x, y, z));
}
}
|
Javascript
function minCost(n, x, y, z) {
let prev1 = 0,
prev2 = 0;
for (let i = 2; i <= n; i++) {
let cur;
if (i % 2 == 1) {
cur = Math.min(prev1 + x + z, prev2 + y);
} else {
cur = Math.min(prev1 + x, prev2 + y);
}
prev1 = prev2;
prev2 = cur;
}
return prev2;
}
let n = 5,
x = 2,
y = 1,
z = 3;
console.log(minCost(n, x, y, z));
|
Time complexity : O(N)
Auxiliary Space : O(1)
Last Updated :
16 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...