Skip to content

Add space optimized 0/1 knapsack implementation (O(W) space)#14537

Open
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
nickzerjeski:feat-14462-space-optimized-knapsack
Open

Add space optimized 0/1 knapsack implementation (O(W) space)#14537
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
nickzerjeski:feat-14462-space-optimized-knapsack

Conversation

@nickzerjeski
Copy link
Copy Markdown

Summary

This PR adds a space-optimized 0/1 knapsack implementation using a single 1D DP array, reducing auxiliary space from O(n * W) to O(W) while preserving O(n * W) time complexity.

What Changed

  • Added knapsack_space_optimized(capacity, weights, values, num_items) -> int to dynamic_programming/knapsack.py.
  • Implemented reverse-capacity iteration per item to preserve 0/1 semantics (each item can be taken at most once).
  • Added type hints and docstring explanations.
  • Added doctests for representative scenarios.
  • Added basic input validation for negative capacity/item count and invalid num_items bounds.

Issue Comments Review

I reviewed issue #14462 comments before implementation. There are currently no comments, so this follows the issue specification directly.

Validation

  • python -m doctest -v dynamic_programming/knapsack.py (pass)
  • Consistency check against existing 2D DP implementation on canonical example: both return 220.

Issue

Closes #14462

Copilot AI review requested due to automatic review settings April 11, 2026 14:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new space-optimized 0/1 knapsack solver to the existing dynamic programming implementations, reducing auxiliary memory from a 2D table to a single 1D DP array while keeping the same time complexity.

Changes:

  • Added knapsack_space_optimized(capacity, weights, values, num_items) -> int using 1D DP with reverse-capacity iteration.
  • Added basic input validation for negative capacity/num_items and num_items bounds.
  • Added doctests for a few representative successful scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +66 to +72
for item_index in range(num_items):
item_weight = weights[item_index]
item_value = values[item_index]
for current_capacity in range(capacity, item_weight - 1, -1):
dp[current_capacity] = max(
dp[current_capacity], item_value + dp[current_capacity - item_weight]
)
Comment on lines +58 to +63
if num_items < 0:
raise ValueError("The number of items cannot be negative.")
if capacity < 0:
raise ValueError("The knapsack capacity cannot be negative.")
if num_items > len(weights) or num_items > len(values):
raise ValueError("The number of items exceeds the provided input lengths.")
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 11, 2026
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add space optimized 0/1 knapsack implementation (O(W) space)

2 participants