703990. Maximum Product Cutting

Easy

Maximum Product Cutting

Slug: maximum-product-cutting4522

Difficulty: Easy

Id: 703990

Topic Tags: Dynamic Programming, Algorithms

Company Tags: None

Summary

The Maximum Product Cutting problem is an optimization challenge that requires finding the maximum product of a set of products obtained by cutting a given string into smaller segments. This problem involves dynamic programming concepts and involves maximizing the product of segment lengths.

Detailed Explanation

To solve this problem, we will use dynamic programming to build up a solution from the bottom up. We will initialize an array dp where dp[i] represents the maximum product that can be obtained by cutting the string into segments of length i.

The base case is when the length of the segment is 1. In this case, the maximum product is simply the value of the character at that position.

For lengths greater than 1, we consider all possible cuts and calculate the maximum product for each cut. We take the maximum of these products as our current dp[i] value.

Finally, we return the maximum dp value which represents the maximum product that can be obtained by cutting the string into segments.

Here's a step-by-step breakdown:

  • Initialize an array dp of size n+1 where n is the length of the input string.
  • Set dp[0] = 1 as the base case for the empty string.
  • For each i from 1 to n,
    • Calculate max_product = 0.
    • For each j from 1 to i,
      • Calculate the product product = dp[j-1] * (string[i-1]).value.
      • Update max_product if product > max_product.
    • Set dp[i] = max_product.

Time complexity: O(n^2) where n is the length of the input string. Space complexity: O(n) for the dynamic programming array.

Optimized Solutions

Java

View on GeeksforGeeks
public class Solution {
    public int maxProduct(String s, int n) {
        int[] dp = new int[n+1];
        dp[0] = 1;
        
        for(int i=1; i<=n; i++) {
            int max_product = 0;
            for(int j=1; j<=i; j++) {
                int product = dp[j-1] * s.charAt(i-j).getValue();
                if(product > max_product) {
                    max_product = product;
                }
            }
            dp[i] = max_product;
        }
        
        return dp[n];
    }

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.