Given a rope of length n (integer), cut it into at least two integer-length pieces so that the product of the piece lengths is maximized. Return the maximum product.
Notes and constraints:
At least one cut must be made (so for n <= 1 the problem is degenerate).
Practical input: n is a positive integer; typical interview constraints use n up to a few thousands.
We present three methods: naive recursion (exponential), dynamic programming (bottom-up O(n^2)), and a mathematical/greedy approach using factor 3 which yields an O(1) / O(n) solution.
For each possible first cut i (1..n-1) either take the product i * (n-i) (stop cutting the second piece) or further cut the remaining piece and take i * MPC(n-i). The best among all i is the answer. This explores all partitions and is exponential.
classSolution {
public:int maxProductCutting(int n) {
if (n ==0|| n ==1) return0;
int ans =0;
for (int i =1; i < n; ++i) {
ans = max(ans, max(i * (n - i), i * maxProductCutting(n - i)));
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
packagemainfuncmaxProductCutting(nint) int {
ifn==0||n==1 { return0 }
ans:=0fori:=1; i < n; i++ {
ans = max(ans, max(i*(n-i), i*maxProductCutting(n-i)))
}
returnans}
func max(a, bint) int { ifa > b { returna }; returnb }
1
2
3
4
5
6
7
8
9
10
classSolution {
publicintmaxProductCutting(int n) {
if (n == 0 || n == 1) return 0;
int ans = 0;
for (int i = 1; i < n; ++i) {
ans = Math.max(ans, Math.max(i * (n - i), i * maxProductCutting(n - i)));
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
from typing import Optional
classSolution:
defmaxProductCutting(self, n: int) -> int:
if n ==0or n ==1:
return0 ans =0for i in range(1, n):
ans = max(ans, max(i * (n - i), i * self.maxProductCutting(n - i)))
return ans
Subproblems overlap; store dp[k] = maximum product obtainable for length k (with at least one cut inside the k segment). Build dp from smaller lengths to larger ones: for each k consider first cut i and combine i with either k-i (no further cut) or dp[k-i] (further cuts).
classSolution {
public:int maxProductCutting(int n) {
if (n <=1) return0;
vector<int> dp(n +1, 0);
dp[0] =0; dp[1] =0;
for (int len =2; len <= n; ++len) {
int best =0;
for (int i =1; i < len; ++i) {
best = max(best, max(i * (len - i), i * dp[len - i]));
}
dp[len] = best;
}
return dp[n];
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
packagemainfuncmaxProductCutting(nint) int {
ifn<=1 { return0 }
dp:= make([]int, n+1)
dp[0], dp[1] = 0, 0forlength:=2; length<=n; length++ {
best:=0fori:=1; i < length; i++ {
best = max(best, max(i*(length-i), i*dp[length-i]))
}
dp[length] = best }
returndp[n]
}
func max(a, bint) int { ifa > b { returna }; returnb }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publicintmaxProductCutting(int n) {
if (n <= 1) return 0;
int[] dp =newint[n + 1];
dp[0]= 0; dp[1]= 0;
for (int len = 2; len <= n; ++len) {
int best = 0;
for (int i = 1; i < len; ++i) {
best = Math.max(best, Math.max(i * (len - i), i * dp[len - i]));
}
dp[len]= best;
}
return dp[n];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from typing import List
classSolution:
defmaxProductCutting(self, n: int) -> int:
if n <=1:
return0 dp: List[int] = [0] * (n +1)
dp[0], dp[1] =0, 0for length in range(2, n +1):
best =0for i in range(1, length):
best = max(best, max(i * (length - i), i * dp[length - i]))
dp[length] = best
return dp[n]
For large n the maximum product is achieved by breaking the integer into as many 3s as possible, with special handling when remainder is 1 (turn a 3+1 into 2+2). This yields an O(1)/O(n) solution depending on implementation.
classSolution {
public:longlong maxProductCutting(int n) {
if (n ==2) return1;
if (n ==3) return2;
longlong cnt3 = n /3;
int rem = n %3;
if (rem ==1) {
cnt3 -=1;
longlong res =1;
while (cnt3--) res *=3;
return res *4;
} elseif (rem ==2) {
longlong res =1;
while (cnt3--) res *=3;
return res *2;
} else {
longlong res =1;
while (cnt3--) res *=3;
return res;
}
}
};
classSolution {
publiclongmaxProductCutting(int n) {
if (n == 2) return 1;
if (n == 3) return 2;
int cnt3 = n / 3;
int rem = n % 3;
if (rem == 1) {
cnt3 -= 1;
long res = 1;
while (cnt3--> 0) res *= 3;
return res * 4;
}
long res = 1;
while (cnt3--> 0) res *= 3;
if (rem == 2) return res * 2;
return res;
}
}
classSolution:
defmaxProductCutting(self, n: int) -> int:
if n ==2:
return1if n ==3:
return2 cnt3 = n //3 rem = n %3if rem ==1:
cnt3 -=1 res =1for _ in range(cnt3):
res *=3return res *4 res =1for _ in range(cnt3):
res *=3if rem ==2:
return res *2return res