Teaching Kids Programming – Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm


Teaching Kids Programming: Videos on Data Structures and Algorithms

You are given a list of integers prices where prices[i] represents the price to sell a rod of size i + 1, and an integer n which represents the current size of the rod.

Given you can cut the rod into any number of different sizes, return the maximum profit that can be earned.

Constraints
m = n ≤ 1000 where m is the length of prices.
Example 1
Input
prices = [1, 3, 5, 7, 7, 7]
n = 6
Output
10
Explanation
The price table shows that we can:
Sell a rod of size 1 for price of 1
Sell a rod of size 2 for price of 3
Sell a rod of size 3 for price of 5
Sell a rod of size 4 for price of 7
Sell a rod of size 5 for price of 7
Sell a rod of size 6 for price of 7
The optimal way to cut the rod is to split it into 2 pieces of length 3, to achieve profit of 10.

Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm

This is a unbounded knapsack problem. There are tex_7ef280cc103443dd89d9c71c62062ae7 Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video items, which are size tex_96da3d606a559569fb4df5c2d8332816 Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video . And the values for each items is tex_f731985281cb7e898a554f751ba4d59e Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video . Given the size tex_0b052539c069727cbdf2c2a2d2bfe458 Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video of the knapsack, we want to choose the items such that the total values is maximized.

tex_9bd41668586ab003d1f896f64458d792 Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video subject to tex_6e9e29a09424123611e0d8ae227e0949 Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video
The tex_46e2de14decefcc44c8d48fa794d8c86 Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video is the count of the item i and we can have as many as we want for each item.

We can use Top Down Dynamic Programming aka Recursion with Memoization where we add @cache to let the computer memorize the values for us. Alternatively, we can use Bottom Up Dynamic Programming algorithm to solve this via the arrays where we proactively store/remember the values.

1
2
3
4
5
6
7
class Solution:
    def maxProfitUnboundedKnapsack(self, prices, n):        
        dp = [0] * (n + 1)
        for i in range(1, n + 1):
            for j in range(i):
                dp[i] = max(dp[i], prices[j] + dp[i - j - 1])
        return dp[-1]
class Solution:
    def maxProfitUnboundedKnapsack(self, prices, n):        
        dp = [0] * (n + 1)
        for i in range(1, n + 1):
            for j in range(i):
                dp[i] = max(dp[i], prices[j] + dp[i - j - 1])
        return dp[-1]

The time complexity is O(N^2) quadratic (e.g. polynomial time). The outer loop i takes N iterations, and the inner loop takes up to i so overall iterations are N+(N-1)+(N-2)+… which is N(N+1)//2 thus O(N^2).

The space complexity is O(N) as we need one additional array to store the DP values e.g. dp[0] to dp[n].

See also this where we can solve this unbouned knapsack problem via Top Down Dynamic Programming Algorithm aka Recursion with Memoization (@cache): Teaching Kids Programming – Max Profit of Rod Cutting (Unbounded Knapsack) via Top Down Dynamic Programming Algorithm

Knapsack Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
820 words
Last Post: Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Top Down Dynamic Programming Algorithm
Next Post: Teaching Kids Programming - Shortest Path Algorithms by Iterative Deepening Search (IDS), Depth First Search (DFS) or Breadth First Search (BFS) on Undirected Graphs

The Permanent URL is: Teaching Kids Programming – Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm

Leave a Reply