Skip to content

Commit 75c17fa

Browse files
committed
add 343. Integer Break
1 parent 72c825f commit 75c17fa

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Math/343_IntegerBreak.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-04 15:01:50
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
Magic factor 2 and 3.
10+
11+
Break the numbers into magic factor only 2 and 3 if number >= 4,
12+
Then we will get the max product.
13+
14+
If we break a number N into two factors x, N-x, product is p=x(N-x).
15+
To get the maximum of p, x=N/2 when N is even, x=(N-1)/2 when N is odd.
16+
If x can be break again and the product is bigger than x, then break recursively.
17+
18+
Now the question is, for a given number N, when to stop break. It's clearly that:
19+
(N/2)*(N/2) < N (N is even), then N < 4, N = 2
20+
(N-1)/2 *(N+1)/2 < N (N id odd), then N < 5, N = 3, N = 1
21+
22+
Thus, the factors of the perfect product should only be 2 or 3.
23+
24+
According to:
25+
https://discuss.leetcode.com/topic/45341/an-simple-explanation-of-the-math-part-and-a-o-n-solution
26+
*/
27+
int integerBreak(int n) {
28+
if(n==2 || n==3){
29+
return n-1;
30+
}
31+
int three_cnt = n / 3;
32+
int two_cnt = (n - n/3 * 3) / 2;
33+
34+
if((n-n/3*3) == 1){
35+
three_cnt -= 1;
36+
two_cnt = 2;
37+
}
38+
return pow(3, three_cnt) * pow(2, two_cnt);
39+
}
40+
};
41+
42+
/*
43+
2
44+
7
45+
10
46+
102
47+
*/

Math/343_IntegerBreak.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# @Last Modified time: 2016-07-04 15:01:41
5+
6+
7+
class Solution(object):
8+
""" Magic factor 2 and 3.
9+
10+
Break the numbers into magic factor only 2 and 3 if number >= 4,
11+
Then we will get the max product.
12+
13+
If we break a number N into two factors x, N-x, product is p=x(N-x).
14+
To get the maximum of p, x=N/2 when N is even, x=(N-1)/2 when N is odd.
15+
If x can be break again and the product is bigger than x, then break recursively.
16+
17+
Now the question is, for a given number N, when to stop break. It's clearly that:
18+
(N/2)*(N/2) < N (N is even), then N < 4, N = 2
19+
(N-1)/2 *(N+1)/2 < N (N id odd), then N < 5, N = 3, N = 1
20+
21+
Thus, the factors of the perfect product should only be 2 or 3.
22+
23+
According to:
24+
https://discuss.leetcode.com/topic/45341/an-simple-explanation-of-the-math-part-and-a-o-n-solution
25+
"""
26+
def integerBreak(self, n):
27+
assert(n >= 2)
28+
if n == 2 or n == 3:
29+
return n - 1
30+
three_cnt = n / 3
31+
two_cnt = (n - three_cnt * 3) / 2
32+
33+
# We should minus one 3 and add two 2, number may be 10, 13
34+
if n - three_cnt * 3 == 1:
35+
two_cnt = 2
36+
three_cnt -= 1
37+
return 3 ** three_cnt * (2 ** two_cnt)
38+
39+
"""
40+
2
41+
7
42+
10
43+
102
44+
"""

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
* 326. [Power of Three](Math/326_PowerOfThree.py)
275275
* 319. [Bulb Switcher](Math/319_BulbSwitcher.py)
276276
* 335. [Self Crossing](Math/335_SelfCrossing.py)
277+
* 343. [Integer Break](Math/343_IntegerBreak.py)
277278

278279
# [Bit Manipulation](BitManipulation/)
279280

0 commit comments

Comments
 (0)