Skip to content

Commit 6c4e43d

Browse files
authored
Create maximize-number-of-nice-divisors.py
1 parent 4605ea0 commit 6c4e43d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
# variant of "343. integer break"
5+
class Solution(object):
6+
def maxNiceDivisors(self, primeFactors):
7+
"""
8+
:type primeFactors: int
9+
:rtype: int
10+
"""
11+
# given a1 + a2 + ... + ak <= n, find max of a1 * a2 * ... * ak
12+
# => given a1 + a2 + ... + ak = n, find max of a1 * a2 * ... * ak
13+
# => ai is either 3 or 2, see proof in "343. integer break"
14+
MOD = 10**9 + 7
15+
if primeFactors <= 3:
16+
return primeFactors
17+
if primeFactors % 3 == 0: # 6 => 3*3
18+
return pow(3, primeFactors//3, MOD)
19+
if primeFactors % 3 == 1: # 4 => 2*2
20+
return (2*2*pow(3, (primeFactors-4)//3, MOD)) % MOD
21+
return (2*pow(3, (primeFactors-2)//3, MOD)) % MOD # 5 => 2*3

0 commit comments

Comments
 (0)