-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegerBreak.py
More file actions
36 lines (28 loc) · 785 Bytes
/
integerBreak.py
File metadata and controls
36 lines (28 loc) · 785 Bytes
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
"""
Given an integer n, break it into the sum of k positive integers, where k >= 2, and
maximize the product of those integers.
Return the maximum product you can get.
Example 1:
Input: n = 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.
# Solved using the idea that when you break down a number into it twos and threes, the product of these will be the
maximum
"""
class Solution:
def integerBreak(self, n: int) -> int:
if n == 0:
return 0
if n <= 3:
return n-1
threes = n // 3
remainder = n % 3
if remainder == 1:
threes -= 1
twos = 2
elif remainder == 2:
twos = 1
else:
twos = 0
result = (3**threes) * (2**twos)
return result