forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0553-optimal-division.js
More file actions
27 lines (26 loc) · 922 Bytes
/
0553-optimal-division.js
File metadata and controls
27 lines (26 loc) · 922 Bytes
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
/**
* 553. Optimal Division
* https://leetcode.com/problems/optimal-division/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} nums
* @return {string}
*/
var optimalDivision = function(nums) {
if (nums.length === 1) return nums[0].toString();
if (nums.length === 2) return `${nums[0]}/${nums[1]}`;
return `${nums[0]}/(${nums.slice(1).join('/')})`;
};