码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 343. Integer Break

时间:2016-05-19 01:31:17      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/integer-break/

数学题。

技术分享
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Solution {
 5 public:
 6     int integerBreak(int n) {
 7         if (n == 2) return 1;
 8         if (n == 3) return 2;
 9         
10         int product = 1;
11         while (n > 4)
12         {
13               n -= 3;
14               product *= 3;              
15         }
16         product *= n;
17                 
18         return product;
19     }
20 };
21 
22 int main ()
23 {
24     Solution testSolution;
25     int result;
26     
27     result = testSolution.integerBreak(5);    
28     cout << result << endl;
29     
30     char ch;
31     cin >> ch;
32     
33     return 0;
34 }
View Code

也可以用DP,但自己写的DP想错了,想到分解成MAX(N-2 * 2, N-3 * 3), 应该用MAX(MAX, I-J * J)。

https://leetcode.com/discuss/102024/java-greedy-o-logn-simple-code-and-o-n-2-dp

 

LeetCode 343. Integer Break

标签:

原文地址:http://www.cnblogs.com/pegasus923/p/5507138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!