Rod cutting problem

Dynamic Programming DP on subsequences Hard

Given a rod of length N inches and an array price[] where price[i] denotes the value of a piece of rod of length i inches (1-based indexing). Determine the maximum value obtainable by cutting up the rod and selling the pieces. Make any number of cuts, or none at all, and sell the resulting pieces.

Examples:

Input: price = [1, 6, 8, 9, 10, 19, 7, 20], N = 8

Output: 25

Explanation: Cut the rod into lengths of 2 and 6 for a total price of 6 + 19= 25.

Input: price = [1, 5, 8, 9], N = 4

Output: 10

Explanation: Cut the rod into lengths of 2 and 2 for a total price of 5 + 5 = 10.

Input: price = [5, 5, 8, 9, 10, 17, 17, 20], N = 8

Constraints

  • 1 ≤ N ≤ 1000
  • 1 ≤ price[i] ≤ 105

Hints

  • Define dp[i] as the maximum profit obtainable for a rod of length i. the recurrence relation is:dp[i]=max(dp[i],dp[i−j]+price[j−1])
  • Instead of a full 2D DP table, we use a 1D DP array (dp[N]).

Company Tags

Johnson & Johnson Zynga Boston Consulting Group Epic Systems eBay Seagate Technology Oracle Bungie Epic Games Teladoc Health HashiCorp Unity Technologies Morgan Stanley AMD Micron Technology Roblox DoorDash Shopify Red Hat Snowflake Deloitte Instacart McKinsey & Company Walmart Texas Instruments Google Microsoft Amazon Meta Apple Netflix Adobe