Skip to content

Commit 726d8d3

Browse files
committed
the solution of problem "Integer Break"
1 parent 66bca09 commit 726d8d3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LeetCode
1313
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [C++](./algorithms/cpp/topKFrequentElements/topKFrequentElements.cpp)|Medium|
1414
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [C++](./algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp)|Easy|
1515
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) | [C++](./algorithms/cpp/reverseString/ReverseString.cpp)|Easy|
16+
|343|[Integer Break](https://leetcode.com/problems/integer-break/) | [C++](./algorithms/cpp/integerBreak/IntegerBreak.cpp)|Medium|
1617
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](./algorithms/cpp/powerOfFour/PowerOfFour.cpp)|Easy|
1718
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
1819
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Source : https://leetcode.com/problems/integer-break/
2+
// Author : Hao Chen
3+
// Date : 2016-05-29
4+
5+
/***************************************************************************************
6+
*
7+
* Given a positive integer n, break it into the sum of at least two positive integers
8+
* and maximize the product of those integers. Return the maximum product you can get.
9+
*
10+
* For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3
11+
* + 4).
12+
*
13+
* Note: you may assume that n is not less than 2.
14+
*
15+
* There is a simple O(n) solution to this problem.
16+
* You may check the breaking results of n ranging from 7 to 10 to discover the
17+
* regularities.
18+
*
19+
* Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating
20+
* all test cases.
21+
***************************************************************************************/
22+
23+
class Solution {
24+
public:
25+
// As the hint said, checking the n with ranging from 7 to 10 to discover the regularities.
26+
// n = 7, 3*4 = 12
27+
// n = 8, 3*3*2 = 18
28+
// n = 9, 3*3*3 = 27
29+
// n = 10, 3*3*4 = 36
30+
// n = 11, 3*3*3*2 = 54
31+
//
32+
// we can see we can break the number by 3 if it is greater than 4;
33+
//
34+
int integerBreak(int n) {
35+
if ( n == 2 ) return 1;
36+
if ( n == 3 ) return 2;
37+
int result = 1;
38+
while( n > 4 ) {
39+
result *= 3;
40+
n -= 3;
41+
}
42+
result *= n;
43+
return result;
44+
}
45+
};
46+

0 commit comments

Comments
 (0)