Skip to content

Latest commit

 

History

History
51 lines (26 loc) · 1.4 KB

553.md

File metadata and controls

51 lines (26 loc) · 1.4 KB

553. Optimal Division

You are given an integer array nums. The adjacent integers in nums will perform the float division.

For example, for nums = [2,3,4], we will evaluate the expression "2/3/4".

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.

Return the corresponding expression that has the maximum value in string format.

Note: your expression should not contain redundant parenthesis.

How to make the value maximal? for example we have

Input: nums = [1000,100,10,2]

Output: "1000/(100/10/2)"

Explanation:

1000/(100/10/2) = 1000/((100/10)/2) = 200

However, the bold parenthesis in "1000/((100/10)/2)" are redundant, since they don't influence the operation priority. So you should return "1000/(100/10/2)".

Other cases:

1000/(100/10)/2 = 50

1000/(100/(10/2)) = 50

1000/100/10/2 = 0.5

1000/100/(10/2) = 2

interesting, a/b/c != a/(b/c)

100/10/2 = 5

100/5 = 20

But how to define the maximum? It seems we keep using the last and last last two elements first will be the answer? let me see the answer.

1 <= nums.length <= 10

2 <= nums[i] <= 1000

There is only one optimal division for the given iput.

the answer is very straightforward. Because we have to let the right value (divisor) as smaller as possible. Thus the ansewr shall be a/(b/c/d/e)..