-
Notifications
You must be signed in to change notification settings - Fork 5
/
BuyOranges.java
32 lines (30 loc) · 1.09 KB
/
BuyOranges.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*https://practice.geeksforgeeks.org/problems/minimum-cost-to-fill-given-weight-in-a-bag1956/1*/
/*unbounded knapsack variation (knapsack-with-duplicate-items4201 in GFG)*/
class Solution
{
public int minimumCost(int cost[], int N,int W)
{
int[] dp = new int[W+1];
for (int i = 1; i < dp.length; ++i) dp[i] = 1000000000;
for (int i = 0; i <= W; ++i)
for (int j = 0; j < N; ++j)
if ((j+1) <= i && cost[j] != -1) dp[i] = Math.min(dp[i],dp[i-(j+1)]+cost[j]);
return dp[W] == 1000000000 ? -1 : dp[W];
}
}
/*rod cutting variation(cutted-segments1642 in GFG)*/
class Solution
{
public int minimumCost(int cost[], int N,int W)
{
int[] table = new int[W+1];
for (int i = 1; i <= W; ++i)
{
int min = 1000000000;
for (int j = 0; j < Math.min(i,N); ++j)
min = Math.min(min,((i-(j+1) == 0 || (i-(j+1) > 0 && table[i-(j+1)] > 0)) && cost[j] != -1 ? table[i-(j+1)]+cost[j] : 1000000000));
table[i] = min;
}
return table[W] == 1000000000 ? -1 : table[W];
}
}