728x90
"""
The problem is the classic 0-1 Knapsack, where you're given items with specific weights and values,
and you must select a subset that maximizes total value without exceeding a given capacity.
Here, 'i' represents the number (or index) of items considered so far, and 'w' represents
the current weight capacity being evaluated.
"""
def knapsack(benefits, weights, capacity):
n = len(benefits)
dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)]
# building dp table
for i in range(1, n + 1):
for w in range(1, capacity + 1):
if weights[i - 1] <= w:
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + benefits[i - 1])
# if not included, if included
# which one is big ?
else:
# Current item cannot be included, so take the value without it
dp[i][w] = dp[i - 1][w]
# beautiful
return dp[n][capacity], dp
def main():
benefits = [60, 100, 120]
weights = [10, 20, 30]
capacity = 50
max_value, dp_table = knapsack(benefits, weights, capacity)
print("Maximum value:", max_value)
print("table", dp_table)
if __name__ == "__main__":
main()
728x90
'hacking sorcerer' 카테고리의 다른 글
pingudad in a while (0) | 2025.03.14 |
---|---|
generate_password_verify.py (0) | 2025.03.12 |
Crypto exchange security whitepaper and Mitre ATT&CK; for bybit hacking (0) | 2025.03.09 |
better_than_pil_in_one_second (0) | 2025.03.07 |
lexicon yummy! (2) | 2025.03.05 |