Given a positive integern, break it into the sum ofat leasttwo positive integers and maximize the product of those integers. Return the maximum product you can get.
For example, givenn= 2, return 1 (2 = 1 + 1); givenn= 10, return 36 (10 = 3 + 3 + 4).
Note: You may assume thatnis not less than 2 and not larger than 58.
Hint:?There is a simple O(n) solution to this problem.
You may check the breaking results ofnranging from 7 to 10 to discover the regularities.
代碼簡單, 分析過程很有趣!?
當一個數拆分成兩個的時候乘積最大情況, 偶數時: ?n/2 * n/2 >= n , n的最小值 4; 奇數時: (n+1)/2 * (n-1)/2 ?>= n; n==5 , 也就是說當一個數大于等于4的時候 應該有更好的拆分方法。 所以選擇因子應該在1,2,3 中選擇, 顯然1 不需要選, 而同時 3*3 > 2*2*2. ?所以盡量拆分成3 就會得到最大乘積。 ?