# Leetcode 343：整数拆分（最详细的解法！！！）

输入: 2



输入: 10



class Solution:
def integerBreak(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1

result = -1
for i in range(1, n):
result = max(result, i*(n - i), i * self.integerBreak(n - i))

return result


max(result, i*(n - i), i * self.integerBreak(n - i)) ==>
max(result, i * self.integerBreak(n - i))


class Solution:
def integerBreak(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1

mem = [-1 for i in range(n + 1)]
mem[1] = 1
for i in range(2, n + 1):
for j in range(1, i):
mem[i] = max(mem[i], j*(i - j), j*mem[i - j])

return mem[n]


2^3 < 3^2
4^3 < 3^4 > 3^3^1
5^3 < 3^5 < 3^3^2
6^3 < 3^6 < 3^3^3


• δ x n x δ x = n ∗ x n x − 2 ∗ ( 1 − l n ( x ) ) \frac{\delta{x^{\frac{n}{x}}}}{\delta x}=n*x^{\frac{n}{x} -2}*(1-ln(x))

x=e的时候取最大值。所以我们这里可以取的整数就是23，但是2*2*2<3*3，所以就有了前面的结论。

class Solution:
def integerBreak(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 3:
return n - 1

result = 1
while n > 4:
n -= 3
result *= 3

return n * result


05-03 2637

02-23 148
05-21 36
04-19 3433
04-22 187
12-16 112
08-09 1225
01-11 198
05-18 342
03-17 242
03-12 2917
10-18 1万+
05-11 20
04-20 7402