<p>You are given an integer array <code>nums</code>. The adjacent integers in <code>nums</code> will perform the float division.</p>

<ul>
	<li>For example, for <code>nums = [2,3,4]</code>, we will evaluate the expression <code>&quot;2/3/4&quot;</code>.</li>
</ul>

<p>However, you can add any number of parenthesis at any position to change the priority of operations. You want to add these parentheses such the value of the expression after the evaluation is maximum.</p>

<p>Return <em>the corresponding expression that has the maximum value in string format</em>.</p>

<p><strong>Note:</strong> your expression should not contain redundant parenthesis.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1000,100,10,2]
<strong>Output:</strong> &quot;1000/(100/10/2)&quot;
<strong>Explanation:</strong> 1000/(100/10/2) = 1000/((100/10)/2) = 200
However, the bold parenthesis in &quot;1000/(<strong>(</strong>100/10<strong>)</strong>/2)&quot; are redundant since they do not influence the operation priority.
So you should return &quot;1000/(100/10/2)&quot;.
Other cases:
1000/(100/10)/2 = 50
1000/(100/(10/2)) = 50
1000/100/10/2 = 0.5
1000/100/(10/2) = 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,4]
<strong>Output:</strong> &quot;2/(3/4)&quot;
<strong>Explanation:</strong> (2/(3/4)) = 8/3 = 2.667
It can be shown that after trying all possibilities, we cannot get an expression with evaluation greater than 2.667
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10</code></li>
	<li><code>2 &lt;= nums[i] &lt;= 1000</code></li>
	<li>There is only one optimal division for the given input.</li>
</ul>

from pprint import pprint

class Fraction:

    def __init__(self, numerator=0, denominator=1):
        self.numerator = numerator
        self.denominator = denominator

    def __lt__(self, second_fraction):
           return self.numerator * second_fraction.denominator < second_fraction.numerator * self.denominator

    def __le__(self, second_fraction):
           return self.numerator * second_fraction.denominator <= second_fraction.numerator * self.denominator

    def __truediv__(self, second_fraction):
        return Fraction(self.numerator * second_fraction.denominator, self.denominator * second_fraction.numerator)

    def __repr__(self):
        return f"{self.numerator}/{self.denominator}"


class Solution:
    def optimalDivision(self, nums: List[int]) -> str:
        max_value = [[None] * len(nums) for _ in range(len(nums))]
        min_value = [[None] * len(nums) for _ in range(len(nums))]
        max_value_string = [[None] * len(nums) for _ in range(len(nums))]
        min_value_string = [[None] * len(nums) for _ in range(len(nums))]
        def get_bracket_string(numerator, denominator):
            print(numerator, denominator)
            numerator_string = numerator if numerator.count('(') < 1 else f"({numerator})"
            denominator_string = denominator if denominator.count('/') < 1 else f"({denominator})"
            return f"{numerator_string}/{denominator_string}"
        for length in range(len(nums)):
            for left_index in range(len(nums) - length):
                right_index = left_index + length
                if length == 0:
                    min_value[left_index][right_index] = Fraction(nums[left_index])
                    max_value[left_index][right_index] = Fraction(nums[left_index])
                    max_value_string[left_index][right_index] = str(nums[left_index])
                    min_value_string[left_index][right_index] = str(nums[left_index])
                else:
                    for denominator_start_index in range(left_index + 1, right_index + 1):
                        # numerator = left_index ... denominator_start_index - 1
                        # denominator = denominator_start_index ... right_index
                        if max_value[left_index][right_index] is None or max_value[left_index][denominator_start_index - 1] / min_value[denominator_start_index][right_index] > max_value[left_index][right_index]:
                            max_value[left_index][right_index] = max_value[left_index][denominator_start_index - 1] / min_value[denominator_start_index][right_index]
                            max_value_string[left_index][right_index] = get_bracket_string(max_value_string[left_index][denominator_start_index - 1], min_value_string[denominator_start_index][right_index])
                        if min_value[left_index][right_index] is None or min_value[left_index][denominator_start_index - 1] / max_value[denominator_start_index][right_index] <= min_value[left_index][right_index]:
                            min_value[left_index][right_index] = min_value[left_index][denominator_start_index - 1] / max_value[denominator_start_index][right_index]
                            min_value_string[left_index][right_index] = get_bracket_string(min_value_string[left_index][denominator_start_index - 1], max_value_string[denominator_start_index][right_index])
        pprint(max_value)
        pprint(max_value_string)
        pprint(min_value)
        pprint(min_value_string)
        return max_value_string[0][len(nums) - 1]
16900