- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0343-integer-break.rb
41 lines (33 loc) · 819 Bytes
/
0343-integer-break.rb
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
37
38
39
40
41
# frozen_string_literal: true
# 343. Integer Break
# Medium
# https://leetcode.com/problems/integer-break
=begin
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.
Example 2:
Input: n = 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
Constraints:
2 <= n <= 58
=end
# @param {Integer} n
# @return {Integer}
definteger_break(n)
n < 4 ? n - 1 : 3**((n - 2) / 3) * ((n - 2) % 3 + 2)
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_integer_break < Test::Unit::TestCase
deftest_
assert_equal1,integer_break(2)
assert_equal36,integer_break(10)
end
end