You are given a bag of size W kg and you are provided costs of packets different weights of oranges in array cost[] where cost[i] is basically the cost of āiā kg packet of oranges. Where cost[i] = -1 means that āiā kg packet of orange is unavailable
Find the minimum total cost to buy exactly W kg oranges and if it is not possible to buy exactly W kg oranges then print -1. It may be assumed that there is an infinite supply of all available packet types.
Note: array starts from index 1.Ā
Examples:Ā
Input : W = 5, cost[] = {20, 10, 4, 50, 100} Output : 14 We can choose two oranges to minimize cost. First orange of 2Kg and cost 10. Second orange of 3Kg and cost 4. Input : W = 5, cost[] = {1, 10, 4, 50, 100} Output : 5 We can choose five oranges of weight 1 kg. Input : W = 5, cost[] = {1, 2, 3, 4, 5} Output : 5 Costs of 1, 2, 3, 4 and 5 kg packets are 1, 2, 3, 4 and 5 Rs respectively. We choose packet of 5kg having cost 5 for minimum cost to get 5Kg oranges. Input : W = 5, cost[] = {-1, -1, 4, 5, -1} Output : -1 Packets of size 1, 2 and 5 kg are unavailable because they have cost -1. Cost of 3 kg packet is 4 Rs and of 4 kg is 5 Rs. Here we have only weights 3 and 4 so by using these two we can not make exactly W kg weight, therefore answer is -1.
Method 1:Ā
This problem is can be reduced to Unbounded Knapsack. So in the cost array, we first ignore those packets which are not available i.e; cost is -1 and then traverse the cost array and create two array val[] for storing the cost of āiā kg packet of orange and wt[] for storing weight of the corresponding packet. Suppose cost[i] = 50 so the weight of the packet will be i and the cost will be 50.Ā
Algorithm :
- Create matrix min_cost[n+1][W+1], where n is number of distinct weighted packets of orange and W is the maximum capacity of the bag.
- Initialize the 0th row with INF (infinity) and 0th Column with 0.
- Now fill the matrix
- if wt[i-1] > j then min_cost[i][j] = min_cost[i-1][j] ;
- if wt[i-1] <= j then min_cost[i][j] = min(min_cost[i-1][j], val[i-1] + min_cost[i][j-wt[i-1]]);
- If min_cost[n][W]==INF then output will be -1 because this means that we cant not make weight W by using these weights else output will be min_cost[n][W].
Below is the implementation of the above algorithm:
C++
// C++ program to find minimum cost to get exactly // W Kg with given packets #include<bits/stdc++.h> #define INF 1000000 using namespace std; Ā
// cost[] initial cost array including unavailable packet // W capacity of bag int MinimumCost( int cost[], int n, int W) { Ā Ā Ā Ā // val[] and wt[] arrays Ā Ā Ā Ā // val[] array to store cost of 'i' kg packet of orange Ā Ā Ā Ā // wt[] array weight of packet of orange Ā Ā Ā Ā vector< int > val, wt; Ā
Ā Ā Ā Ā // traverse the original cost[] array and skip Ā Ā Ā Ā // unavailable packets and make val[] and wt[] Ā Ā Ā Ā // array. size variable tells the available number Ā Ā Ā Ā // of distinct weighted packets Ā Ā Ā Ā int size = 0; Ā Ā Ā Ā for ( int i=0; i<n; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā if (cost[i]!= -1) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val.push_back(cost[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā wt.push_back(i+1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size++; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā n = size; Ā Ā Ā Ā int min_cost[n+1][W+1]; Ā
Ā Ā Ā Ā // fill 0th row with infinity Ā Ā Ā Ā for ( int i=0; i<=W; i++) Ā Ā Ā Ā Ā Ā Ā Ā min_cost[0][i] = INF; Ā
Ā Ā Ā Ā // fill 0'th column with 0 Ā Ā Ā Ā for ( int i=1; i<=n; i++) Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][0] = 0; Ā
Ā Ā Ā Ā // now check for each weight one by one and fill the Ā Ā Ā Ā // matrix according to the condition Ā Ā Ā Ā for ( int i=1; i<=n; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( int j=1; j<=W; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // wt[i-1]>j means capacity of bag is Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // less than weight of item Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (wt[i-1] > j) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = min_cost[i-1][j]; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // here we check we get minimum cost either Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // by including it or excluding it Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = min(min_cost[i-1][j], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j-wt[i-1]] + val[i-1]); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // exactly weight W can not be made by given weights Ā Ā Ā Ā return (min_cost[n][W]==INF)? -1: min_cost[n][W]; } Ā
// Driver program to run the test case int main() { Ā Ā Ā Ā int cost[] = {1, 2, 3, 4, 5}, W = 5; Ā Ā Ā Ā int n = sizeof (cost)/ sizeof (cost[0]); Ā
Ā Ā Ā Ā cout << MinimumCost(cost, n, W); Ā Ā Ā Ā return 0; } |
Java
// Java Code for Minimum cost to // fill given weight in a bag import java.util.*; Ā
class GFG { Ā Ā Ā Ā Ā Ā Ā Ā Ā // cost[] initial cost array including Ā Ā Ā Ā // unavailable packet W capacity of bag Ā Ā Ā Ā public static int MinimumCost( int cost[], int n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int W) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // val[] and wt[] arrays Ā Ā Ā Ā Ā Ā Ā Ā // val[] array to store cost of 'i' kg Ā Ā Ā Ā Ā Ā Ā Ā // packet of orange wt[] array weight of Ā Ā Ā Ā Ā Ā Ā Ā // packet of orange Ā Ā Ā Ā Ā Ā Ā Ā Vector<Integer> val = new Vector<Integer>(); Ā Ā Ā Ā Ā Ā Ā Ā Vector<Integer> wt = new Vector<Integer>(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // traverse the original cost[] array and skip Ā Ā Ā Ā Ā Ā Ā Ā // unavailable packets and make val[] and wt[] Ā Ā Ā Ā Ā Ā Ā Ā // array. size variable tells the available Ā Ā Ā Ā Ā Ā Ā Ā // number of distinct weighted packets Ā Ā Ā Ā Ā Ā Ā Ā int size = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i < n; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cost[i] != - 1 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val.add(cost[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā wt.add(i + 1 ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā n = size; Ā Ā Ā Ā Ā Ā Ā Ā int min_cost[][] = new int [n+ 1 ][W+ 1 ]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // fill 0th row with infinity Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i <= W; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[ 0 ][i] = Integer.MAX_VALUE; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // fill 0'th column with 0 Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1 ; i <= n; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][ 0 ] = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // now check for each weight one by one and Ā Ā Ā Ā Ā Ā Ā Ā // fill the matrix according to the condition Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1 ; i <= n; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 1 ; j <= W; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // wt[i-1]>j means capacity of bag is Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // less than weight of item Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (wt.get(i- 1 ) > j) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = min_cost[i- 1 ][j]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // here we check we get minimum cost Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // either by including it or excluding Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // it Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = Math.min(min_cost[i- 1 ][j], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j-wt.get(i- 1 )] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val.get(i- 1 )); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // exactly weight W can not be made by Ā Ā Ā Ā Ā Ā Ā Ā // given weights Ā Ā Ā Ā Ā Ā Ā Ā return (min_cost[n][W] == Integer.MAX_VALUE)? - 1 : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[n][W]; Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā /* Driver program to test above function */ Ā Ā Ā Ā public static void main(String[] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā int cost[] = { 1 , 2 , 3 , 4 , 5 }, W = 5 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int n = cost.length; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(MinimumCost(cost, n, W)); Ā Ā Ā Ā } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Python program to find minimum cost to get exactly # W Kg with given packets Ā
INF = 1000000 Ā
# cost[] initial cost array including unavailable packet # W capacity of bag def MinimumCost(cost, n, W): Ā
Ā Ā Ā Ā # val[] and wt[] arrays Ā Ā Ā Ā # val[] array to store cost of 'i' kg packet of orange Ā Ā Ā Ā # wt[] array weight of packet of orange Ā Ā Ā Ā val = list () Ā Ā Ā Ā wt = list () Ā
Ā Ā Ā Ā # traverse the original cost[] array and skip Ā Ā Ā Ā # unavailable packets and make val[] and wt[] Ā Ā Ā Ā # array. size variable tells the available number Ā Ā Ā Ā # of distinct weighted packets. Ā Ā Ā Ā size = 0 Ā Ā Ā Ā for i in range (n): Ā Ā Ā Ā Ā Ā Ā Ā if (cost[i] ! = - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val.append(cost[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā wt.append(i + 1 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size + = 1 Ā
Ā Ā Ā Ā n = size Ā Ā Ā Ā min_cost = [[ 0 for i in range (W + 1 )] for j in range (n + 1 )] Ā
Ā Ā Ā Ā # fill 0th row with infinity Ā Ā Ā Ā for i in range (W + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā min_cost[ 0 ][i] = INF Ā
Ā Ā Ā Ā # fill 0th column with 0 Ā Ā Ā Ā for i in range ( 1 , n + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][ 0 ] = 0 Ā
Ā Ā Ā Ā # now check for each weight one by one and fill the Ā Ā Ā Ā # matrix according to the condition Ā Ā Ā Ā for i in range ( 1 , n + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā for j in range ( 1 , W + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # wt[i-1]>j means capacity of bag is Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # less than weight of item Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (wt[i - 1 ] > j): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = min_cost[i - 1 ][j] Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # here we check we get minimum cost either Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # by including it or excluding it Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = min (min_cost[i - 1 ][j], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j - wt[i - 1 ]] + val[i - 1 ]) Ā
Ā Ā Ā Ā # exactly weight W can not be made by given weights Ā Ā Ā Ā if (min_cost[n][W] = = INF): Ā Ā Ā Ā Ā Ā Ā Ā return - 1 Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā return min_cost[n][W] Ā
# Driver program to run the test case cost = [ 1 , 2 , 3 , 4 , 5 ] W = 5 n = len (cost) Ā
print (MinimumCost(cost, n, W)) Ā
# This code is contributed by Soumen Ghosh. |
C#
// C# Code for Minimum cost to // fill given weight in a bag Ā
using System; using System.Collections.Generic; Ā
class GFG { Ā Ā Ā Ā Ā Ā Ā Ā Ā // cost[] initial cost array including Ā Ā Ā Ā // unavailable packet W capacity of bag Ā Ā Ā Ā public static int MinimumCost( int []cost, int n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int W) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // val[] and wt[] arrays Ā Ā Ā Ā Ā Ā Ā Ā // val[] array to store cost of 'i' kg Ā Ā Ā Ā Ā Ā Ā Ā // packet of orange wt[] array weight of Ā Ā Ā Ā Ā Ā Ā Ā // packet of orange Ā Ā Ā Ā Ā Ā Ā Ā List< int > val = new List< int >(); Ā Ā Ā Ā Ā Ā Ā Ā List< int > wt = new List< int >(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // traverse the original cost[] array and skip Ā Ā Ā Ā Ā Ā Ā Ā // unavailable packets and make val[] and wt[] Ā Ā Ā Ā Ā Ā Ā Ā // array. size variable tells the available Ā Ā Ā Ā Ā Ā Ā Ā // number of distinct weighted packets Ā Ā Ā Ā Ā Ā Ā Ā int size = 0; Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i < n; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cost[i] != -1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val.Add(cost[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā wt.Add(i + 1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā n = size; Ā Ā Ā Ā Ā Ā Ā Ā int [,]min_cost = new int [n+1,W+1]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // fill 0th row with infinity Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i <= W; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[0,i] = int .MaxValue; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // fill 0'th column with 0 Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1; i <= n; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i,0] = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // now check for each weight one by one and Ā Ā Ā Ā Ā Ā Ā Ā // fill the matrix according to the condition Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1; i <= n; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 1; j <= W; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // wt[i-1]>j means capacity of bag is Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // less than weight of item Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (wt[i-1] > j) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i,j] = min_cost[i-1,j]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // here we check we get minimum cost Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // either by including it or excluding Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // it Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i,j] = Math.Min(min_cost[i-1,j], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i,j-wt[i-1]] + val[i-1]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // exactly weight W can not be made by Ā Ā Ā Ā Ā Ā Ā Ā // given weights Ā Ā Ā Ā Ā Ā Ā Ā return (min_cost[n,W] == int .MaxValue )? -1: min_cost[n,W]; Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā /* Driver program to test above function */ Ā Ā Ā Ā public static void Main() Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int []cost = {1, 2, 3, 4, 5}; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int W = 5; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int n = cost.Length; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(MinimumCost(cost, n, W)); Ā Ā Ā Ā } } // This code is contributed by Ryuga |
PHP
<?php // PHP program to find minimum cost to // get exactly W Kg with given packets $INF = 1000000; Ā
// cost[] initial cost array including // unavailable packet W capacity of bag function MinimumCost(& $cost , $n , $W ) { Ā Ā Ā Ā global $INF ; Ā Ā Ā Ā Ā Ā Ā Ā Ā // val[] and wt[] arrays Ā Ā Ā Ā // val[] array to store cost of 'i' Ā Ā Ā Ā // kg packet of orange Ā Ā Ā Ā // wt[] array weight of packet of orange Ā Ā Ā Ā $val = array (); Ā Ā Ā Ā $wt = array (); Ā
Ā Ā Ā Ā // traverse the original cost[] array Ā Ā Ā Ā // and skip unavailable packets and Ā Ā Ā Ā // make val[] and wt[] array. size Ā Ā Ā Ā // variable tells the available number Ā Ā Ā Ā // of distinct weighted packets Ā Ā Ā Ā $size = 0; Ā Ā Ā Ā for ( $i = 0; $i < $n ; $i ++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā if ( $cost [ $i ] != -1) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array_push ( $val , $cost [ $i ]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array_push ( $wt , $i + 1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $size ++; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā $n = $size ; Ā Ā Ā Ā $min_cost = array_fill (0, $n + 1, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array_fill (0, $W + 1, NULL)); Ā
Ā Ā Ā Ā // fill 0th row with infinity Ā Ā Ā Ā for ( $i = 0; $i <= $W ; $i ++) Ā Ā Ā Ā Ā Ā Ā Ā $min_cost [0][ $i ] = $INF ; Ā
Ā Ā Ā Ā // fill 0'th column with 0 Ā Ā Ā Ā for ( $i = 1; $i <= $n ; $i ++) Ā Ā Ā Ā Ā Ā Ā Ā $min_cost [ $i ][0] = 0; Ā
Ā Ā Ā Ā // now check for each weight one by Ā Ā Ā Ā // one and fill the matrix according Ā Ā Ā Ā // to the condition Ā Ā Ā Ā for ( $i = 1; $i <= $n ; $i ++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( $j = 1; $j <= $W ; $j ++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // wt[i-1]>j means capacity of bag Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // is less than weight of item Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ( $wt [ $i - 1] > $j ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $min_cost [ $i ][ $j ] = $min_cost [ $i - 1][ $j ]; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // here we check we get minimum Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // cost either by including it Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // or excluding it Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $min_cost [ $i ][ $j ] = min( $min_cost [ $i - 1][ $j ], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $min_cost [ $i ][ $j - $wt [ $i - 1]] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $val [ $i - 1]); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // exactly weight W can not be made Ā Ā Ā Ā // by given weights Ā Ā Ā Ā if ( $min_cost [ $n ][ $W ] == $INF ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return -1; Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā return $min_cost [ $n ][ $W ]; } Ā
// Driver Code $cost = array (1, 2, 3, 4, 5); $W = 5; $n = sizeof( $cost ); echo MinimumCost( $cost , $n , $W ); Ā
// This code is contributed by ita_c ?> |
Javascript
<script> Ā Ā Ā Ā // Javascript program to find minimum cost to get exactly Ā Ā Ā Ā // W Kg with given packets Ā Ā Ā Ā Ā Ā Ā Ā Ā let INF = 1000000; Ā Ā Ā Ā Ā Ā Ā Ā Ā // cost[] initial cost array including unavailable packet Ā Ā Ā Ā // W capacity of bag Ā Ā Ā Ā function MinimumCost(cost, n, W) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // val[] and wt[] arrays Ā Ā Ā Ā Ā Ā Ā Ā // val[] array to store cost of 'i' kg packet of orange Ā Ā Ā Ā Ā Ā Ā Ā // wt[] array weight of packet of orange Ā Ā Ā Ā Ā Ā Ā Ā let val = [], wt = []; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // traverse the original cost[] array and skip Ā Ā Ā Ā Ā Ā Ā Ā // unavailable packets and make val[] and wt[] Ā Ā Ā Ā Ā Ā Ā Ā // array. size variable tells the available number Ā Ā Ā Ā Ā Ā Ā Ā // of distinct weighted packets Ā Ā Ā Ā Ā Ā Ā Ā let size = 0; Ā Ā Ā Ā Ā Ā Ā Ā for (let i=0; i<n; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cost[i]!= -1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val.push(cost[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā wt.push(i+1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā n = size; Ā Ā Ā Ā Ā Ā Ā Ā let min_cost = new Array(n+1); Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < n + 1; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i] = new Array(W + 1); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // fill 0th row with infinity Ā Ā Ā Ā Ā Ā Ā Ā for (let i=0; i<=W; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[0][i] = INF; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // fill 0'th column with 0 Ā Ā Ā Ā Ā Ā Ā Ā for (let i=1; i<=n; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][0] = 0; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // now check for each weight one by one and fill the Ā Ā Ā Ā Ā Ā Ā Ā // matrix according to the condition Ā Ā Ā Ā Ā Ā Ā Ā for (let i=1; i<=n; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let j=1; j<=W; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // wt[i-1]>j means capacity of bag is Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // less than weight of item Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (wt[i-1] > j) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = min_cost[i-1][j]; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // here we check we get minimum cost either Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // by including it or excluding it Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j] = Math.min(min_cost[i-1][j], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost[i][j-wt[i-1]] + val[i-1]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // exactly weight W can not be made by given weights Ā Ā Ā Ā Ā Ā Ā Ā return (min_cost[n][W]==INF)? -1: min_cost[n][W]; Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā // Driver code Ā Ā Ā Ā let cost = [1, 2, 3, 4, 5], W = 5; Ā Ā Ā Ā let n = cost.length; Ā Ā Ā Ā Ā Ā document.write(MinimumCost(cost, n, W)); Ā Ā Ā Ā Ā Ā Ā Ā Ā // This code is contributed by suresh07. </script> |
5
Time Complexity: O(N*W)
Auxiliary Space: O(N*W)
Space Optimized Solution If we take a closer look at this problem, we may notice that this is a variation of Rod Cutting Problem. Instead of doing maximization, here we need to do minimization.
C++
// C++ program to find minimum cost to // get exactly W Kg with given packets #include<bits/stdc++.h> using namespace std; Ā
/* Returns the best obtainable price for Ā Ā Ā a rod of length n and price[] as prices Ā Ā Ā of different pieces */ int minCost( int cost[], int n) { Ā Ā Ā int dp[n+1]; Ā Ā Ā dp[0] = 0; Ā Ā Ā Ā Ā Ā // Build the table val[] in bottom up Ā Ā Ā // manner and return the last entry Ā Ā Ā // from the table Ā Ā Ā for ( int i = 1; i<=n; i++) Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā int min_cost = INT_MAX; Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < i; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cost[j]!=-1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost = min(min_cost, cost[j] + dp[i-j-1]); Ā Ā Ā Ā Ā Ā Ā dp[i] = min_cost; Ā Ā Ā } Ā Ā Ā Ā Ā Ā return dp[n]; } Ā
/* Driver code */ int main() { Ā Ā Ā int cost[] = {20, 10, 4, 50, 100}; Ā Ā Ā int W = sizeof (cost)/ sizeof (cost[0]); Ā Ā Ā cout << minCost(cost, W); Ā Ā Ā return 0; } |
Java
// Java program to find minimum cost to // get exactly W Kg with given packets import java.util.*; class Main { Ā Ā Ā Ā Ā Ā Ā /* Returns the best obtainable price for Ā Ā Ā Ā a rod of length n and price[] as prices Ā Ā Ā Ā of different pieces */ Ā Ā Ā Ā public static int minCost( int cost[], int n) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā int dp[] = new int [n + 1 ]; Ā Ā Ā Ā Ā Ā Ā dp[ 0 ] = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Build the table val[] in bottom up Ā Ā Ā Ā Ā Ā Ā // manner and return the last entry Ā Ā Ā Ā Ā Ā Ā // from the table Ā Ā Ā Ā Ā Ā Ā for ( int i = 1 ; i <= n; i++) Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int min_cost = Integer.MAX_VALUE; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0 ; j < i; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cost[j]!=- 1 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost = Math.min(min_cost, cost[j] + dp[i - j - 1 ]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i] = min_cost; Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[n]; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā public static void main(String[] args) { Ā Ā Ā Ā Ā Ā Ā int cost[] = { 10 ,- 1 ,- 1 ,- 1 ,- 1 }; Ā Ā Ā Ā Ā Ā Ā int W = cost.length; Ā Ā Ā Ā Ā Ā Ā System.out.print(minCost(cost, W)); Ā Ā Ā Ā } } Ā
// This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program to find minimum cost to # get exactly W Kg with given packets import sys Ā
# Returns the best obtainable price for # a rod of length n and price[] as prices # of different pieces def minCost(cost, n): Ā Ā Ā Ā Ā Ā Ā Ā Ā dp = [ 0 for i in range (n + 1 )] Ā
Ā Ā Ā Ā # Build the table val[] in bottom up Ā Ā Ā Ā Ā Ā Ā # manner and return the last entry Ā Ā Ā Ā Ā Ā Ā # from the table Ā Ā Ā Ā for i in range ( 1 , n + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā min_cost = sys.maxsize Ā
Ā Ā Ā Ā Ā Ā Ā Ā for j in range (i): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if cost[j]! = - 1 : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost = min (min_cost, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cost[j] + dp[i - j - 1 ]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i] = min_cost Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[n] Ā
# Driver code cost = [ 10 , - 1 , - 1 , - 1 , - 1 ] W = len (cost) Ā
print (minCost(cost, W)) Ā
# This code is contributed by rag2127 |
C#
// C# program to find minimum cost to // get exactly W Kg with given packets using System; class GFG { Ā Ā Ā Ā Ā Ā Ā Ā Ā /* Returns the best obtainable price for Ā Ā Ā Ā a rod of length n and price[] as prices Ā Ā Ā Ā of different pieces */ Ā Ā Ā Ā static int minCost( int [] cost, int n) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā int [] dp = new int [n + 1]; Ā Ā Ā Ā Ā Ā Ā dp[0] = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Build the table val[] in bottom up Ā Ā Ā Ā Ā Ā Ā // manner and return the last entry Ā Ā Ā Ā Ā Ā Ā // from the table Ā Ā Ā Ā Ā Ā Ā for ( int i = 1; i <= n; i++) Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int min_cost = Int32.MaxValue; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < i; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cost[j]!=-1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost = Math.Min(min_cost, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cost[j] + dp[i - j - 1]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i] = min_cost; Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[n]; Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā // Driver code Ā Ā static void Main() { Ā Ā Ā int [] cost = {20, 10, 4, 50, 100}; Ā Ā Ā int W = cost.Length; Ā Ā Ā Console.Write(minCost(cost, W)); Ā Ā } } Ā
// This code is contributed by divyesh072019 |
Javascript
<script> Ā
Ā Ā Ā Ā // Javascript program to find minimum cost to Ā Ā Ā Ā // get exactly W Kg with given packets Ā Ā Ā Ā Ā Ā Ā Ā Ā /* Returns the best obtainable price for Ā Ā Ā Ā Ā a rod of length n and price[] as prices Ā Ā Ā Ā Ā of different pieces */ Ā Ā Ā Ā function minCost(cost, n) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā let dp = new Array(n+1); Ā Ā Ā Ā Ā Ā Ā dp[0] = 0; Ā
Ā Ā Ā Ā Ā Ā Ā // Build the table val[] in bottom up Ā Ā Ā Ā Ā Ā Ā // manner and return the last entry Ā Ā Ā Ā Ā Ā Ā // from the table Ā Ā Ā Ā Ā Ā Ā for (let i = 1; i<=n; i++) Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let min_cost = Number.MAX_VALUE; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let j = 0; j < i; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (j < n) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_cost = Math.min(min_cost, cost[j] + dp[i-j-1]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i] = min_cost; Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā return dp[n]; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā let cost = [20, 10, 4, 50, 100]; Ā Ā Ā Ā let W = cost.length; Ā Ā Ā Ā document.write(minCost(cost, W)); Ā Ā Ā Ā Ā </script> |
14
Time Complexity: O(N2)
Auxiliary Space: O(N)
Top Down Approach: We can also solve the problem using memoization.
C++
// C++ program to find minimum cost to // get exactly W Kg with given packets #include <bits/stdc++.h> using namespace std; Ā
int helper(vector< int >& cost, vector< int >& weight, int n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int w, vector<vector< int > >& dp) { Ā Ā Ā Ā // base cases Ā Ā Ā Ā if (w == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā if (w < 0 or n <= 0) Ā Ā Ā Ā Ā Ā Ā Ā return INT_MAX; Ā
Ā Ā Ā Ā if (dp[n][w] != -1) Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w]; Ā
Ā Ā Ā Ā if (cost[n - 1] < 0) Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = min(INT_MAX, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1, w, dp)); Ā
Ā Ā Ā Ā if (weight[n - 1] <= w) { Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = min(cost[n - 1] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + helper(cost, weight, n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā w - weight[n - 1], dp), Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1, w, dp)); Ā Ā Ā Ā } Ā Ā Ā Ā return dp[n][w] = helper(cost, weight, n - 1, w, dp); } int minCost(vector< int >& cost, int W) { Ā Ā Ā Ā int N = cost.size(); Ā Ā Ā Ā // Your code goes here Ā Ā Ā Ā vector< int > weight(N); Ā
Ā Ā Ā Ā // create the weight array Ā Ā Ā Ā for ( int i = 1; i <= N; i++) { Ā Ā Ā Ā Ā Ā Ā Ā weight[i - 1] = i; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // initialize the dp array Ā Ā Ā Ā vector<vector< int > > dp(N + 1, vector< int >(W + 1, -1)); Ā
Ā Ā Ā Ā int res = helper(cost, weight, N, W, dp); Ā
Ā Ā Ā Ā // return -1 if result is MAX Ā Ā Ā Ā return (res == INT_MAX) ? -1 : res; } Ā
/* Driver code */ int main() { Ā Ā Ā Ā vector< int > cost = { 20, 10, 4, 50, 100 }; Ā Ā Ā Ā int W = cost.size(); Ā Ā Ā Ā cout << minCost(cost, W); Ā Ā Ā Ā return 0; } |
Java
// Java program to find minimum cost to // get exactly W Kg with given packets import java.io.*; Ā
class GFG { Ā
Ā Ā Ā Ā public static int helper( int cost[], int weight[], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int n, int w, int dp[][]) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // base cases Ā Ā Ā Ā Ā Ā Ā Ā if (w == 0 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0 ; Ā Ā Ā Ā Ā Ā Ā Ā if (w < 0 || n <= 0 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return Integer.MAX_VALUE; Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (dp[n][w] != - 1 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w]; Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (cost[n - 1 ] < 0 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] = Math.min( Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Integer.MAX_VALUE, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1 , w, dp)); Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (weight[n - 1 ] <= w) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] = Math.min( Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cost[n - 1 ] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + helper(cost, weight, n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā w - weight[n - 1 ], dp), Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1 , w, dp)); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = helper(cost, weight, n - 1 , w, dp); Ā Ā Ā Ā } Ā Ā Ā Ā public static int minCost( int cost[], int W) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int N = cost.length; Ā Ā Ā Ā Ā Ā Ā Ā int weight[] = new int [N]; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // create the weight array Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1 ; i <= N; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā weight[i - 1 ] = i; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // initialize the dp array Ā Ā Ā Ā Ā Ā Ā Ā int dp[][] = new int [N + 1 ][W + 1 ]; Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i < N + 1 ; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0 ; j < W + 1 ; j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = - 1 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā int res = helper(cost, weight, N, W, dp); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // return -1 if result is MAX Ā Ā Ā Ā Ā Ā Ā Ā return (res == Integer.MAX_VALUE) ? - 1 : res; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā public static void main(String[] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int cost[] = { 20 , 10 , 4 , 50 , 100 }; Ā Ā Ā Ā Ā Ā Ā Ā int W = cost.length; Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(minCost(cost, W)); Ā Ā Ā Ā } } Ā
// This code is contributed by Rohit Pradhan |
Python3
# Python3 program to find minimum cost to # get exactly W Kg with given packets import sys Ā
def helper(cost, weight, n, w, dp): Ā
Ā Ā Ā Ā # base cases Ā Ā Ā Ā if (w = = 0 ): Ā Ā Ā Ā Ā Ā Ā Ā return 0 Ā Ā Ā Ā if (w < 0 or n < = 0 ): Ā Ā Ā Ā Ā Ā Ā Ā return sys.maxsize Ā
Ā Ā Ā Ā if (dp[n][w] ! = - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā
Ā Ā Ā Ā if (cost[n - 1 ] < 0 ): Ā Ā Ā Ā Ā Ā Ā Ā dp[n][w] = min (sys.maxsize, helper(cost, weight, n - 1 , w, dp)) Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā
Ā Ā Ā Ā if (weight[n - 1 ] < = w): Ā Ā Ā Ā Ā Ā Ā Ā dp[n][w] = min (cost[n - 1 ] + helper(cost, weight, n, w - weight[n - 1 ], dp), helper(cost, weight, n - 1 , w, dp)) Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[n][w] = helper(cost, weight, n - 1 , w, dp) Ā Ā Ā Ā return dp[n][w] Ā
def minCost(cost, W): Ā
Ā Ā Ā Ā N = len (cost) Ā Ā Ā Ā Ā Ā Ā Ā Ā weight = [ 0 for i in range (N)] Ā
Ā Ā Ā Ā # create the weight array Ā Ā Ā Ā for i in range ( 1 ,N + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā weight[i - 1 ] = i Ā
Ā Ā Ā Ā # initialize the dp array Ā Ā Ā Ā dp = [[ - 1 for i in range (W + 1 )] for j in range (N + 1 )] Ā
Ā Ā Ā Ā res = helper(cost, weight, N, W, dp) Ā
Ā Ā Ā Ā # return -1 if result is MAX Ā Ā Ā Ā return - 1 if (res = = sys.maxsize) else res Ā
# Driver code cost = [ 20 , 10 , 4 , 50 , 100 ] W = len (cost) print (minCost(cost, W)) Ā
# This code is contributed by shinjanpatra |
C#
// C# program to find minimum cost to // get exactly W Kg with given packets using System; Ā
class GFG { Ā Ā static int helper( int [] cost, int [] weight, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int n, int w, int [,] dp) Ā Ā { Ā Ā Ā Ā // base cases Ā Ā Ā Ā if (w == 0) Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā if (w < 0 || n <= 0) Ā Ā Ā Ā Ā Ā return Int32.MaxValue; Ā
Ā Ā Ā Ā if (dp[n,w] != -1) Ā Ā Ā Ā Ā Ā return dp[n,w]; Ā
Ā Ā Ā Ā if (cost[n - 1] < 0) Ā Ā Ā Ā Ā Ā return dp[n,w] = Math.Min( Ā Ā Ā Ā Ā Ā Int32.MaxValue, Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1, w, dp)); Ā
Ā Ā Ā Ā if (weight[n - 1] <= w) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā return dp[n,w] = Math.Min( Ā Ā Ā Ā Ā Ā Ā Ā cost[n - 1] Ā Ā Ā Ā Ā Ā Ā Ā + helper(cost, weight, n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā w - weight[n - 1], dp), Ā Ā Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1, w, dp)); Ā Ā Ā Ā } Ā Ā Ā Ā return dp[n,w] Ā Ā Ā Ā Ā Ā = helper(cost, weight, n - 1, w, dp); Ā Ā } Ā
Ā Ā static int minCost( int [] cost, int W) Ā Ā { Ā Ā Ā Ā int N = cost.Length; Ā Ā Ā Ā int [] weight = new int [N]; Ā
Ā Ā Ā Ā // create the weight array Ā Ā Ā Ā for ( int i = 1; i <= N; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā weight[i - 1] = i; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // initialize the dp array Ā Ā Ā Ā int [,] dp = new int [N + 1, W + 1]; Ā Ā Ā Ā for ( int i = 0; i < N + 1; i++) Ā Ā Ā Ā Ā Ā for ( int j = 0; j < W + 1; j++) Ā Ā Ā Ā Ā Ā Ā Ā dp[i,j] = -1; Ā
Ā Ā Ā Ā int res = helper(cost, weight, N, W, dp); Ā
Ā Ā Ā Ā // return -1 if result is MAX Ā Ā Ā Ā return (res == Int32.MaxValue) ? -1 : res; Ā Ā } Ā
Ā Ā // Driver Code Ā Ā static public void Main() Ā Ā { Ā Ā Ā Ā int [] cost = { 20, 10, 4, 50, 100 }; Ā Ā Ā Ā int W = cost.Length; Ā Ā Ā Ā Console.Write(minCost(cost, W)); Ā Ā } } Ā
// This code is contributed by kothavvsaakash |
Javascript
<script> Ā
// JavaScript program to find minimum cost to // get exactly W Kg with given packets function helper(cost, weight, n, w, dp) { Ā Ā Ā Ā // base cases Ā Ā Ā Ā if (w == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā if (w < 0 || n <= 0) Ā Ā Ā Ā Ā Ā Ā Ā return Number.MAX_VALUE; Ā
Ā Ā Ā Ā if (dp[n][w] != -1) Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w]; Ā
Ā Ā Ā Ā if (cost[n - 1] < 0) Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = Math.min(Number.MAX_VALUE, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1, w, dp)); Ā
Ā Ā Ā Ā if (weight[n - 1] <= w) { Ā Ā Ā Ā Ā Ā Ā Ā return dp[n][w] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = Math.min(cost[n - 1] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + helper(cost, weight, n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā w - weight[n - 1], dp), Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā helper(cost, weight, n - 1, w, dp)); Ā Ā Ā Ā } Ā Ā Ā Ā return dp[n][w] = helper(cost, weight, n - 1, w, dp); } function minCost(cost,W) { Ā Ā Ā Ā let N = cost.length; Ā Ā Ā Ā // Your code goes here Ā Ā Ā Ā let weight = new Array(N); Ā
Ā Ā Ā Ā // create the weight array Ā Ā Ā Ā for (let i = 1; i <= N; i++) { Ā Ā Ā Ā Ā Ā Ā Ā weight[i - 1] = i; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // initialize the dp array Ā Ā Ā Ā let dp = new Array(N + 1).fill(-1).map(()=> new Array(W + 1).fill(-1)); Ā
Ā Ā Ā Ā let res = helper(cost, weight, N, W, dp); Ā
Ā Ā Ā Ā // return -1 if result is MAX Ā Ā Ā Ā return (res == Number.MAX_VALUE) ? -1 : res; } Ā
/* Driver code */ let cost = [ 20, 10, 4, 50, 100 ]; let W = cost.length; document.write(minCost(cost, W), "</br>" ); Ā
// This code is contributed by shinjanpatra Ā
</script> |
14
Time Complexity: O(N*W)
Auxiliary Space: O(N*W)
This article is contributed by Shashank Mishra ( Gullu ).This article is reviewed by team GeeksForGeeks.Ā
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Ā
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!