Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
Example 1:
Example 2:
Note: You may assume that n is not less than 2 and not larger than 58.
Because the maximum input is not larger than 58, so we can allocate a fixed-length array to store the result of each number we known.
The array is initialized as follows:
Assume the input number N, split then N into L and R.
Assume that N = 8, L = 1 and R = 7.
NOTE: In the above, the 5 can split into 2 and 3 that product is greater than 5, so, if L or R equals the 5 we can replace it into 2 and 3. The 3 although can consist of 2 and 1, but the product is lower than 3, so we don't replace it.
See the full solution.
leetcode
Medium
Math
Dynamic Programming