-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
feat: add UnboundedKnapsack algorithm #1428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,36 @@ | ||
/** | ||
* @function unboundedKnapsack | ||
* @description Solve the Unbounded Knapsack problem using Dynamic Programming. | ||
* @param {number[]} weights - An array of item weights. | ||
* @param {number[]} values - An array of item values. | ||
* @param {number} capacity - The maximum capacity of the knapsack. | ||
* @return {number} The maximum value that can be obtained. | ||
* @see [UnboundedKnapsack](https://en.wikipedia.org/wiki/Knapsack_problem#Unbounded_knapsack_problem) | ||
*/ | ||
|
||
function unboundedKnapsack(weights, values, capacity) { | ||
const n = weights.length; | ||
|
||
// Create a DP array to store the maximum value for each possible knapsack capacity. | ||
const dp = new Array(capacity + 1).fill(0); | ||
|
||
// Loop through each possible knapsack capacity from 0 to the maximum capacity. | ||
for (let w = 0; w <= capacity; w++) { | ||
// Loop through each item in the given items. | ||
for (let i = 0; i < n; i++) { | ||
// Check if the weight of the current item is less than or equal to the current knapsack capacity. | ||
if (weights[i] <= w) { | ||
// Update the DP array with the maximum value between not taking the current item | ||
// and taking the current item and adding its value. | ||
dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]); | ||
} | ||
} | ||
} | ||
|
||
// The final element of the DP array stores the maximum value that can be obtained with | ||
// the given knapsack capacity. | ||
return dp[capacity]; | ||
} | ||
|
||
export { unboundedKnapsack }; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.