Skip to content

Commit 6c0e38b

Browse files
committed
added 5kyu - Simple Fun #339 - Maximum Product 2
1 parent 26227a1 commit 6c0e38b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: 5kyu_SimpleFun#339_MaximumProduct2.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 5kyu - Simple Fun #339: Maximum Product 2
2+
3+
// Task
4+
// Given an positive integer n(5 <= n <= 100). Your task is to divide n into some different integers, maximize the product of these integers.
5+
6+
// Example
7+
// For n = 5, the output should be 6.
8+
9+
// 5 can divide into 2 and 3, 2 x 3 = 6
10+
11+
// For n = 8, the output should be 15.
12+
13+
// 8 can divide into 3 and 5, 3 x 5 = 15
14+
15+
// For n = 10, the output should be 30.
16+
17+
// 10 can divide into 2, 3 and 5, 2 x 3 x 5 = 30
18+
19+
// For n = 15, the output should be 144.
20+
21+
// 15 can divide into 2, 3, 4 and 6, 2 x 3 x 4 x 6 = 144
22+
23+
function maximumProduct(n) {
24+
function fn(m, i, a) {
25+
if (m === 0) return a;
26+
if (m < 0 || i > m) return 0;
27+
return Math.max(fn(m-i, i+1, a*i), fn(m, i+1, a));
28+
}
29+
return fn(n, 2, 1);
30+
}

0 commit comments

Comments
 (0)