Techno Blender
Digitally Yours.

Minimum cost to reduce given number to less than equal to zero

0 30


Given array A[] and B[] of size N representing N type of operations. Given a number H, reduce this number to less than equal to 0 by performing the following operation at minimum cost. Choose ith operation and subtract A[i] from H and the cost incurred will be B[i]. Every operation can be performed any number of times. 

Examples:

Input: A[] = {8, 4, 2}, B[] = {3, 2, 1}, H = 9 
Output: 4
Explanation: The optimal way to solve this problem is to decrease the number H = 9 by the first operation reducing it by A[1] = 8 and the cost incurred is B[1] = 3. H is now 1. Use the third operation to reduce H = 1 by A[3] = 2 cost incurred will be B[3] = 1. Now H is  -1 which is less than equal to 0 hence. in cost = 3 + 1 = 4 number H can be made less than equal to 0.

Input: A[] = {1, 2, 3, 4, 5, 6}, B[] = {1, 3, 9, 27, 81, 243}, H = 100
Output: 100
Explanation: It is optimal to use the first operation 100 times to make H zero in minimum cost.

Naive approach: The basic way to solve the problem is as follows:

The basic way to solve this problem is to generate all possible combinations by using a recursive approach.

Time Complexity: O(HN)
Auxiliary Space: O(1)

Efficient Approach:  The above approach can be optimized based on the following idea:

Dynamic programming can be used to solve this problem

  • dp[i] represents a minimum cost to make I zero from given operations
  • recurrence relation: dp[i] = min(dp[i], dp[max(0, i – A[i])] + B[i])

Follow the steps below to solve the problem:

  • Declare a dp table of size H + 1 with all values initialized to infinity
  • Base case dp[0] = 0
  • Iterate from 1 to H to calculate a value for each of them and to do that use all operations from 0 to j and try to make i zero by the minimum cost of these operations.
  • Finally, return minimum cost dp[H]

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

int findMinimumCost(int A[], int B[], int N, int H)

{

  

    

    

    vector<int> dp(H + 1, INT_MAX);

  

    

    dp[0] = 0;

  

    

    

    for (int i = 1; i <= H; i++) {

  

        for (int j = 0; j < N; j++) {

            dp[i] = min(dp[i], dp[max(0, i - A[j])] + B[j]);

        }

    }

  

    

    return dp[H];

}

  

int main()

{

    

    int A[] = { 8, 4, 2 }, B[] = { 3, 2, 1 }, H = 9;

    int N = sizeof(A) / sizeof(A[0]);

  

    

    cout << findMinimumCost(A, B, N, H) << endl;

  

    

    int A1[] = { 1, 2, 3, 4, 5, 6 },

        B1[] = { 1, 3, 9, 27, 81, 243 }, H1 = 100;

    int N1 = sizeof(A1) / sizeof(A1[0]);

  

    

    cout << findMinimumCost(A1, B1, N1, H1) << endl;

  

    return 0;

}

Time Complexity: O(N * H)
Auxiliary Space: O(N * H)

Related Articles:


Given array A[] and B[] of size N representing N type of operations. Given a number H, reduce this number to less than equal to 0 by performing the following operation at minimum cost. Choose ith operation and subtract A[i] from H and the cost incurred will be B[i]. Every operation can be performed any number of times. 

Examples:

Input: A[] = {8, 4, 2}, B[] = {3, 2, 1}, H = 9 
Output: 4
Explanation: The optimal way to solve this problem is to decrease the number H = 9 by the first operation reducing it by A[1] = 8 and the cost incurred is B[1] = 3. H is now 1. Use the third operation to reduce H = 1 by A[3] = 2 cost incurred will be B[3] = 1. Now H is  -1 which is less than equal to 0 hence. in cost = 3 + 1 = 4 number H can be made less than equal to 0.

Input: A[] = {1, 2, 3, 4, 5, 6}, B[] = {1, 3, 9, 27, 81, 243}, H = 100
Output: 100
Explanation: It is optimal to use the first operation 100 times to make H zero in minimum cost.

Naive approach: The basic way to solve the problem is as follows:

The basic way to solve this problem is to generate all possible combinations by using a recursive approach.

Time Complexity: O(HN)
Auxiliary Space: O(1)

Efficient Approach:  The above approach can be optimized based on the following idea:

Dynamic programming can be used to solve this problem

  • dp[i] represents a minimum cost to make I zero from given operations
  • recurrence relation: dp[i] = min(dp[i], dp[max(0, i – A[i])] + B[i])

Follow the steps below to solve the problem:

  • Declare a dp table of size H + 1 with all values initialized to infinity
  • Base case dp[0] = 0
  • Iterate from 1 to H to calculate a value for each of them and to do that use all operations from 0 to j and try to make i zero by the minimum cost of these operations.
  • Finally, return minimum cost dp[H]

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

int findMinimumCost(int A[], int B[], int N, int H)

{

  

    

    

    vector<int> dp(H + 1, INT_MAX);

  

    

    dp[0] = 0;

  

    

    

    for (int i = 1; i <= H; i++) {

  

        for (int j = 0; j < N; j++) {

            dp[i] = min(dp[i], dp[max(0, i - A[j])] + B[j]);

        }

    }

  

    

    return dp[H];

}

  

int main()

{

    

    int A[] = { 8, 4, 2 }, B[] = { 3, 2, 1 }, H = 9;

    int N = sizeof(A) / sizeof(A[0]);

  

    

    cout << findMinimumCost(A, B, N, H) << endl;

  

    

    int A1[] = { 1, 2, 3, 4, 5, 6 },

        B1[] = { 1, 3, 9, 27, 81, 243 }, H1 = 100;

    int N1 = sizeof(A1) / sizeof(A1[0]);

  

    

    cout << findMinimumCost(A1, B1, N1, H1) << endl;

  

    return 0;

}

Time Complexity: O(N * H)
Auxiliary Space: O(N * H)

Related Articles:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment