-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path343.integer-break.cpp
70 lines (68 loc) · 1.44 KB
/
343.integer-break.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* @lc app=leetcode id=343 lang=cpp
*
* [343] Integer Break
*/
/*
*
not work: n=44, p=12或者16
我的思路:
n - sum
p - p个数
max - 最大积
原理: 均分的时候积最大
for i in p
x = int(n/i)
delta = n - (x*(i-1))
out = pow(x,i-1) * delta
if(out < max)
return out
网上的思路: 分成2和3的时候最大
https://leetcode.com/problems/integer-break/discuss/80689/A-simple-explanation-of-the-math-part-and-a-O(n)-solution
*/
#include <complex>
#include <cmath>
#include <iostream>
// @lc code=start
class Solution
{
public:
int integerBreak(int n)
// {
// double p = 2.0;
// int max = 0;
// while (p <= n)
// {
// int x = round(n / p);
// int delta = n - x * (p - 1);
// double out = pow(x, p - 1) * delta;
// if (out > max)
// max = out;
// std::cout << p << "\t" << out << std::endl;
// p++;
// }
// return max;
// }
{
if (n == 2)
return 1;
if (n == 3)
return 2;
int product = 1;
while (n > 4)
{
product *= 3;
n -= 3;
}
product *= n;
return product;
}
};
// @lc code=end
int main()
{
Solution s;
// std::cout << s.integerBreak(2) << std::endl;
// std::cout << s.integerBreak(10) << std::endl;
std::cout << s.integerBreak(44) << std::endl;
}