├── .gitignore ├── LeetCode solutions.md ├── README.md └── standalone_solutions └── knapsack.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode Solutions 2 | A Markdown file that contains my solutions of LeetCode problems, with comments so everyone can understand my thought process and the logic. The solutions file will contain optimal solution, plus any other different method that I came up with that differs from the published LeetCode solution. I have set up a script that will automatically update the the markdown file with each problem I solve, so please check back frequently if you cannot find some problems. 3 | -------------------------------------------------------------------------------- /standalone_solutions/knapsack.py: -------------------------------------------------------------------------------- 1 | def knapsack(n: int, capacity: int, weights: list[int], profit: list[float]) -> int: 2 | # create a dp store that stores how much profit we can accrue for partial_capacity upto full_capacity. 3 | # for example, initially, we will have everything set to 0. This means that using 0 items, our profit for 4 | # all weights ranging from 0 to capacity will be 0. We shall use this to compute the profit that we can 5 | # accrue using the next item. When we try to find profit using item 2, we utilize this dp table by checking 6 | # checking profit values from the end since we do in place update of the same table. 7 | dp = [0 for _ in range(capacity+1)] 8 | 9 | # for all items 1 through n 10 | for item in range(1, n+1): 11 | # we only need the previous row to remember the maximum profit using one less item. We can use that 12 | # to compute the next row, so in order to prevent overwriting of values while use the same array, iterate in reverse 13 | # for each of the partial_capacity in reverse order (stop at 0 since no need to check it) 14 | for partial_capacity in range(capacity, 0, -1): 15 | # consider this item only if the weight of the item is less than the bag capacity. -1 because we are indexing from 1 16 | if weights[item - 1] <= capacity: 17 | # the max profit will be calculated by either using this item or not using. 18 | # if we don't use it, then it will simply be whatever the old profit was. 19 | # if we use it, then it will be simply value of that item + (max profit by subtracting item weight from bag capacity 20 | # and checking our dp table for the profit that we could make from the remaining bag capacity) 21 | dp[partial_capacity] = max(dp[partial_capacity], profit[item - 1] + dp[partial_capacity - weights[item-1]]) 22 | return dp[capacity] 23 | 24 | if __name__ == '__main__': 25 | profit = [60, 100, 120] 26 | weights = [10, 20, 30] 27 | capacity = 50 28 | n = len(profit) 29 | print(knapsack(n, capacity, weights, profit)) --------------------------------------------------------------------------------