leetcode 553. Optimal Division

257 篇文章 17 订阅

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.
我自己写了几个例子,发现最大的情况一直都是A[0] / ( A[1] / A[2] / ... / A[N-1] ),虽然我也不能证明。然后我就这么写,想看看有没有wrong answer的,结果居然AC了。。

public class Optimal_Division_553 {
	
	public String optimalDivision(int[] nums) {
		String s="";
		if(nums.length==1){
			return s+nums[0];
		}
		if(nums.length==2){
			return s+nums[0]+"/"+nums[1];
		}
		
		s+=nums[0]+"/(";
		s+=nums[1];
		for(int i=2;i<nums.length;i++){
			s+="/"+nums[i];
		}
		s+=")";
		return s;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Optimal_Division_553 o=new Optimal_Division_553();
		int[] a=new int[]{1000,100,10,2};
		System.out.println(o.optimalDivision(a));
	}
}

再看大神的解答,有人给出了说明:

X1/X2/X3/../Xn will always be equal to (X1/X2) * Y, no matter how you place parentheses(圆括号). i.e no matter how you place parentheses, X1 always goes to the numerator(分子)and X2 always goes to the denominator(分母). Hence you just need to maximize Y. And Y is maximized when it is equal to X3 *..*Xn. So the answer is always X1/(X2/X3/../Xn) = (X1 *X3 *..*Xn)/X2
为什么X1/X2/X3/../Xn will always be equal to (X1/X2) * Y呢,因为就算X1/(X2/X3/X4),也会得到其等于X1*X3*X4/X2。如下图。

还有一个人这么说,我觉得也有点道理:

As you said, if the input is [x0, x1, ....], x0 will always be numerator(分子), so we should make the denominator(分母) as small as possible to get the maximum result.
Since all the numbers are positive integer, it is clear that x1/x2/../xn results in the smallest denominator(分母).

另外,有真大神用了递归来做,虽然因为考虑到所有可能性,时间复杂度不高。但是,能写出递归做出圆满解答的人,我是超级佩服的!

public class Solution {
    class Result {
        String str;
        double val;
    }
    
    public String optimalDivision(int[] nums) {
        int len = nums.length;
        return getMax(nums, 0, len - 1).str;
    }
    
    private Result getMax(int[] nums, int start, int end) {
        Result r = new Result();
        r.val = -1.0;
        
        if (start == end) {
            r.str = nums[start] + "";
            r.val = (double)nums[start];
        }
        else if (start + 1 == end) {
            r.str = nums[start] + "/" + nums[end];
            r.val = (double)nums[start] / (double)nums[end];
        }
        else {
            for (int i = start; i < end; i++) {
                Result r1 = getMax(nums, start, i);
                Result r2 = getMin(nums, i + 1, end);
                if (r1.val / r2.val > r.val) {
                    r.str = r1.str + "/" + (end - i >= 2 ? "(" + r2.str + ")" : r2.str);
                    r.val = r1.val / r2.val;
                }
            }
        }
        
        //System.out.println("getMax " + start + " " + end + "->" + r.str + ":" + r.val);
        return r;
    }
    
    private Result getMin(int[] nums, int start, int end) {
        Result r = new Result();
        r.val = Double.MAX_VALUE;
        
        if (start == end) {
            r.str = nums[start] + "";
            r.val = (double)nums[start];
        }
        else if (start + 1 == end) {
            r.str = nums[start] + "/" + nums[end];
            r.val = (double)nums[start] / (double)nums[end];
        }
        else {
            for (int i = start; i < end; i++) {
                Result r1 = getMin(nums, start, i);
                Result r2 = getMax(nums, i + 1, end);
                if (r1.val / r2.val < r.val) {
                    r.str = r1.str + "/" + (end - i >= 2 ? "(" + r2.str + ")" : r2.str);
                    r.val = r1.val / r2.val;
                }
            }
        }
        
        //System.out.println("getMin " + start + " " + end + "->" + r.str + ":" + r.val);
        return r;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值