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:

  1. Double the number of blocks present in the container and cost for this operation is X.
  2. Increase the number of blocks present in the container by one and cost for this operation is Y.
  3. 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:

    CPP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to Minimum cost
    // to build N blocks from one block
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // Function to calculate
    // min cost to build N blocks
    int minCost(int n, int x, int y, int z)
    {
        int dp[n + 1] = { 0 };
      
        // Initialize base case
        dp[0] = dp[1] = 0;
      
        for (int i = 2; i <= n; i++) {
            // Recurence when
            // i is odd
            if (i % 2 == 1) {
                dp[i] = min(
                    dp[(i + 1) / 2] + x + z,
                    dp[i - 1] + y);
            }
            // Recurence when
            // i is even
            else {
                dp[i] = min(dp[i / 2] + x,
                            dp[i - 1] + y);
            }
        }
      
        return dp[n];
    }
      
    // Driver code
    int main()
    {
        int n = 5, x = 2, y = 1, z = 3;
        cout << minCost(n, x, y, z) << endl;
      
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to Minimum cost
    // to build N blocks from one block
    class GFG
    {
      
    // Function to calculate
    // min cost to build N blocks
    static int minCost(int n, int x, int y, int z)
    {
        int dp[] = new int[n + 1];
      
        // Initialize base case
        dp[0] = dp[1] = 0;
      
        for (int i = 2; i <= n; i++)
        {
            // Recurence when
            // i is odd
            if (i % 2 == 1)
            {
                dp[i] = Math.min(
                    dp[(i + 1) / 2] + x + z,
                    dp[i - 1] + y);
            }
            // Recurence when
            // i is even
            else 
            {
                dp[i] = Math.min(dp[i / 2] + x,
                            dp[i - 1] + y);
            }
        }
      
        return dp[n];
    }
      
    // Driver code
    public static void main(String[] args)
    {
        int n = 5, x = 2, y = 1, z = 3;
        System.out.print(minCost(n, x, y, z) +"\n");
      
    }
    }
      
    // This code is contributed by Rajput-Ji

    chevron_right

    
    

    Python

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # python3 program to Minimum cost
    # to build N blocks from one block
      
    # Function to calculate
    # min cost to build N blocks
    def minCost(n, x, y, z):
        dp = [0] * (n + 1)
      
        # Initialize base case
        dp[0] = dp[1] = 0
      
        for i in range(2, n + 1):
              
            # Recurence when
            # i is odd
            if (i % 2 == 1):
                dp[i] = min(dp[(i + 1) // 2] + x + z, dp[i - 1] + y)
              
            # Recurence when
            # i is even
            else:
                dp[i] = min(dp[i // 2] + x, dp[i - 1] + y)
      
        return dp[n]
      
    # Driver code
    n = 5
    x = 2
    y = 1
    z = 3
    print(minCost(n, x, y, z))
      
    # This code is contributed by mohit kumar 29

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to Minimum cost
    // to build N blocks from one block
    using System;
      
    class GFG
    {
      
    // Function to calculate
    // min cost to build N blocks
    static int minCost(int n, int x, int y, int z)
    {
        int []dp = new int[n + 1];
      
        // Initialize base case
        dp[0] = dp[1] = 0;
      
        for (int i = 2; i <= n; i++)
        {
            // Recurence when
            // i is odd
            if (i % 2 == 1)
            {
                dp[i] = Math.Min(
                    dp[(i + 1) / 2] + x + z,
                    dp[i - 1] + y);
            }
              
            // Recurence when
            // i is even
            else
            {
                dp[i] = Math.Min(dp[i / 2] + x,
                            dp[i - 1] + y);
            }
        }
        return dp[n];
    }
      
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5, x = 2, y = 1, z = 3;
        Console.Write(minCost(n, x, y, z) +"\n");
    }
    }
      
    // This code is contributed by PrinciRaj1992

    chevron_right

    
    

    Output:

    4
    



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.