LeetCode Optimal Division

原题链接在这里:https://leetcode.com/problems/optimal-division/description/

题目:

Given a list of positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4.

However, you can add any number of parenthesis at any position to change the priority of operations. You should find out how to add parenthesis to get the maximum result, and return the corresponding expression in string format. Your expression should NOT contain redundant parenthesis.

Example:

Input: [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

Note:

  1. The length of the input array is [1, 10].
  2. Elements in the given array will be in range [2, 1000].
  3. There is only one optimal division for each test case.

题解:

backtracking. a/b 最大需要尽可能最大化a 同时最小化b.

dfs的终止条件是只有一个或者两个元素.

Time Complexity: exponential.

Space: O(nums.length). stack space.

AC Java:

 1 class Solution {
 2     class Result{
 3         double val;
 4         String str;
 5     }
 6     public String optimalDivision(int[] nums) {
 7         return getMax(nums, 0, nums.length-1).str;
 8     }
 9     
10     private Result getMax(int [] nums, int l, int r){
11         Result result = new Result();
12         result.val = Double.MIN_VALUE;
13         if(l == r){
14             result.val = nums[l];
15             result.str = String.valueOf(nums[l]);
16             return result;
17         }
18         
19         if(l+1 == r){
20             result.val = (double)nums[l] / (double)nums[r];
21             result.str = nums[l] + "/" + nums[r];
22             return result;
23         }
24         
25         for(int i = l; i<r; i++){
26             Result r1 = getMax(nums, l, i);
27             Result r2 = getMin(nums, i+1, r);
28             if(r1.val/r2.val > result.val){
29                 result.val = r1.val/r2.val;
30                 result.str = r1.str + "/" + (r-(i+1) == 0 ? r2.str : "("+r2.str+")");
31             }
32         }
33         return result;
34     }
35     
36     private Result getMin(int [] nums, int l, int r){
37         Result result = new Result();
38         result.val = Double.MAX_VALUE;
39         if(l == r){
40             result.val = nums[l];
41             result.str = String.valueOf(nums[l]);
42             return result;
43         }
44         
45         if(l+1 == r){
46             result.val = (double)nums[l] / (double)nums[r];
47             result.str = nums[l] + "/" + nums[r];
48             return result;
49         }
50         
51         for(int i = l; i<r; i++){
52             Result r1 = getMin(nums, l, i);
53             Result r2 = getMax(nums, i+1, r);
54             if(r1.val/r2.val < result.val){
55                 result.val = r1.val/r2.val;
56                 result.str = r1.str + "/" + (r-(i+1) == 0 ? r2.str : "("+r2.str+")");
57             }
58         }
59         return result;
60     }
61 }

a/b最大时a要最大, b要最小. 

每个数都大于1, 所以除个数肯定不比自己本上大. 所以a就是第一个数.

越除越小, 所以每个数都不能放过. b就是剩下的每个数以此相除.

Time Complexity: O(nums.length).

Space: O(nums.length).

AC Java:

 1 class Solution {
 2     public String optimalDivision(int[] nums) {
 3         StringBuilder sb = new StringBuilder();
 4         if(nums == null || nums.length == 0){
 5             return sb.toString();
 6         }
 7         
 8         if(nums.length == 1){
 9             return nums[0]+"";
10         }
11         if(nums.length == 2){
12             return nums[0] + "/" + nums[1];
13         }
14         
15         sb.append(nums[0]+"/("+nums[1]);
16         for(int i = 2; i<nums.length; i++){
17             sb.append("/"+nums[i]);
18         }
19         
20         return sb.append(")").toString();
21     }
22 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7721739.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值