| 1 | 1 | def maximum_value(maximum_weight: int, items: list[dict[str, int]]) -> int: |
| 2 | pass | |
| 2 | # Edge Case: Empty items list | |
| 3 | if not items: | |
| 4 | return 0 | |
| 5 | ||
| 6 | # Edge Case: Maximum weight is zero | |
| 7 | if maximum_weight == 0: | |
| 8 | return 0 | |
| 9 | ||
| 10 | n = len(items) | |
| 11 | # Create a 2D array for dynamic programming | |
| 12 | # dp[i][w] represents the maximum value with first i items and weight limit w | |
| 13 | dp = [[0 for _ in range(maximum_weight + 1)] for _ in range(n + 1)] | |
| 14 | ||
| 15 | # Fill the dp table | |
| 16 | for i in range(1, n + 1): | |
| 17 | weight = items[i - 1]["weight"] | |
| 18 | value = items[i - 1]["value"] | |
| 19 | ||
| 20 | for w in range(maximum_weight + 1): | |
| 21 | # Don't take the item | |
| 22 | dp[i][w] = dp[i - 1][w] | |
| 23 | ||
| 24 | # Take the item if it fits | |
| 25 | if weight <= w: | |
| 26 | dp[i][w] = max(dp[i][w], dp[i - 1][w - weight] + value) | |
| 27 | ||
| 28 | return dp[n][maximum_weight] | |
| 29 | # Handled Edge Cases: empty items list, knapsack capacity of zero |