553. Optimal Division Medium
1/**
2 * [553] Optimal Division
3 *
4 * You are given an integer array nums. The adjacent integers in nums will perform the float division.
5 *
6 * For example, for nums = [2,3,4], we will evaluate the expression "2/3/4".
7 *
8 * 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.
9 * Return the corresponding expression that has the maximum value in string format.
10 * Note: your expression should not contain redundant parenthesis.
11 *
12 * <strong class="example">Example 1:
13 *
14 * Input: nums = [1000,100,10,2]
15 * Output: "1000/(100/10/2)"
16 * Explanation: 1000/(100/10/2) = 1000/((100/10)/2) = 200
17 * However, the bold parenthesis in "1000/((100/10)/2)" are redundant since they do not influence the operation priority.
18 * So you should return "1000/(100/10/2)".
19 * Other cases:
20 * 1000/(100/10)/2 = 50
21 * 1000/(100/(10/2)) = 50
22 * 1000/100/10/2 = 0.5
23 * 1000/100/(10/2) = 2
24 *
25 * <strong class="example">Example 2:
26 *
27 * Input: nums = [2,3,4]
28 * Output: "2/(3/4)"
29 * Explanation: (2/(3/4)) = 8/3 = 2.667
30 * It can be shown that after trying all possibilities, we cannot get an expression with evaluation greater than 2.667
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= nums.length <= 10
36 * 2 <= nums[i] <= 1000
37 * There is only one optimal division for the given input.
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/optimal-division/
43// discuss: https://leetcode.com/problems/optimal-division/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn optimal_division(nums: Vec<i32>) -> String {
49 String::new()
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_553() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.