-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from srpriyanshi6/newPR
Solved issue #196.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |