Skip to content

Commit 219d349

Browse files
committedMar 6, 2025
Add solution #553
1 parent 613539c commit 219d349

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@
438438
547|[Number of Provinces](./0547-number-of-provinces.js)|Medium|
439439
551|[Student Attendance Record I](./0551-student-attendance-record-i.js)|Easy|
440440
552|[Student Attendance Record II](./0552-student-attendance-record-ii.js)|Hard|
441+
553|[Optimal Division](./0553-optimal-division.js)|Medium|
441442
557|[Reverse Words in a String III](./0557-reverse-words-in-a-string-iii.js)|Easy|
442443
560|[Subarray Sum Equals K](./0560-subarray-sum-equals-k.js)|Medium|
443444
563|[Binary Tree Tilt](./0563-binary-tree-tilt.js)|Easy|

‎solutions/0553-optimal-division.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 553. Optimal Division
3+
* https://leetcode.com/problems/optimal-division/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums. The adjacent integers in nums will perform
7+
* the float division.
8+
* - For example, for nums = [2,3,4], we will evaluate the expression "2/3/4".
9+
*
10+
* However, you can add any number of parenthesis at any position to change the priority
11+
* of operations. You want to add these parentheses such the value of the expression after
12+
* the evaluation is maximum.
13+
*
14+
* Return the corresponding expression that has the maximum value in string format.
15+
*
16+
* Note: your expression should not contain redundant parenthesis.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {string}
22+
*/
23+
var optimalDivision = function(nums) {
24+
if (nums.length === 1) return nums[0].toString();
25+
if (nums.length === 2) return `${nums[0]}/${nums[1]}`;
26+
return `${nums[0]}/(${nums.slice(1).join('/')})`;
27+
};

0 commit comments

Comments
 (0)
Please sign in to comment.