Skip to content

Commit

Permalink
Merge pull request #197 from srpriyanshi6/newPR
Browse files Browse the repository at this point in the history
Solved issue #196.
  • Loading branch information
yesiamrajeev authored Oct 30, 2024
2 parents e16c1af + 92cfbae commit 34e2971
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions C++/Dynanic Programming/UnbondedKnapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Unbounded Knapsack using DP in C++


#include <bits/stdc++.h>
using namespace std;

// Function to solve the unbounded knapsack problem
int unboundedKnapsack(int n, int W, vector<int>& val, vector<int>& wt) {
vector<int> cur(W + 1, 0); // Create a vector to store the current DP state

// Base Condition
for (int i = wt[0]; i <= W; i++) {
cur[i] = (i / wt[0]) * val[0]; // Calculate the maximum value for the first item
}

for (int ind = 1; ind < n; ind++) {
for (int cap = 0; cap <= W; cap++) {
int notTaken = cur[cap]; // Maximum value without taking the current item

int taken = INT_MIN;
if (wt[ind] <= cap)
taken = val[ind] + cur[cap - wt[ind]]; // Maximum value by taking the current item

cur[cap] = max(notTaken, taken); // Store the maximum value in the current DP state
}
}

return cur[W]; // Return the maximum value considering all items and the knapsack capacity
}

int main() {
vector<int> wt = {2, 4, 6}; // Weight of items
vector<int> val = {5, 11, 13}; // Value of items
int W = 10; // Weight capacity of the knapsack
int n = wt.size(); // Number of items

// Call the function to calculate and output the maximum value the thief can steal
cout << "The Maximum value of items the thief can steal is " << unboundedKnapsack(n, W, val, wt) << endl;

return 0; // Return 0 to indicate successful program execution
}



// Time Complexity : O(n*w)
// Space Complexity : O(w)

0 comments on commit 34e2971

Please sign in to comment.