r/CS_Questions • • Aug 27 '17

Create Maximum Arithmetic Expression

Your input is a double array, and you can use *, +, -, and () parenthesis anywhere to create and output the maximum possible value.

Ex: input is {3,4,5,1} --> output: 72

input is {1,1,1,5} --> output: 15

Follow up, if there are numbers <0

My solution N3 top down dynamic programming approach but I believe there is an O(n) algorithm that solves it. Anyone have any ideas?

        public static long max(int[] nums) {
    
            Long[][] memo = new Long[nums.length][nums.length];
            return max(nums, 0, nums.length - 1, memo);
    
        }
    
        public static long max(int[] nums, int i, int j, Long[][] memo) {
    
            if (memo[i][j] != null) return memo[i][j];
    
            if (i == j) {
                return nums[i];
            }
    
            long max = Integer.MIN_VALUE;
    
            for (int k = i; k < j; k++) {
    
                long left = max(nums, i, k, memo);
                long right = max(nums, k + 1, j, memo);
    
                for (char c : "*-+".toCharArray()) {
                    long res = apply(c, left, right);
                    max = Math.max(max, res);
                }
            }
    
            memo[i][j] = max;
    
            return max;
        }
9 Upvotes

2 comments sorted by

View all comments

1

u/ChanelsUnderworld Oct 13 '17
	public static double largestNum(Double [] n)
{
    ArrayList<Double> list = new ArrayList(Arrays.asList(n));
    
    int a = 0;
    
    while( a < list.size())
    {
    	Double d = list.get(a);
    	if(d < 2.0)
    	{
    		if(a == 0)  //if first element
    		{
    			if(list.size() > 1) //if there's a second element
    				list.set(a+1, d + list.get(a+1));
    		} else if(a == list.size() -1) //if last element
    			list.set(a-1, d + list.get(a-1));
    		else  //non edge-cases. increment smaller neighbor
    		{
    			Double prev = list.get(a-1);
    			Double next = list.get(a+1);
    			
    			if(prev > next)
    				list.set(a+1, next + d);
    			else
    				list.set(a-1, prev + d);
    		}
    		
    		list.remove(a);	//remove tiny value
    	}else
    		a++;
    }
    double product = list.get(0);
    
    for(a = 1; a < list.size(); a++)
    	product *= list.get(a);
    
	return product;
}