[lintcode medium]Maximum Subarray Difference

Maximum Subarray Difference

Given an array with integers.

Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest.

Return the largest difference.

Example

For [1, 2, -3, 1], return 6.

Note

The subarray should contain at least one number

Challenge

O(n) time and O(n) space.

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: An integer indicate the value of maximum difference between two
     *          Subarrays
     */
    public int maxDiffSubArrays(int[] nums) {
        // write your code here
        if(nums==null ||nums.length==0) return 0;

        int n=nums.length;
        int res=0;

        int[] leftMax=new int[n];
        int[] leftMin=new int[n];

        int sumMax=nums[0];
        int sumMin=nums[0];
        leftMax[0]=sumMax;
        leftMin[0]=sumMin;

        for(int i=1;i<n;i++)
        {
            sumMax=Math.max(sumMax+nums[i],nums[i]);
            leftMax[i]=Math.max(leftMax[i-1],sumMax);

            sumMin=Math.min(sumMin+nums[i],nums[i]);
            leftMin[i]=Math.min(leftMin[i-1],sumMin);
        }

        int[] rightMax=new int[n];
        int[] rightMin=new int[n];
        sumMax=nums[n-1];
        sumMin=nums[n-1];

        rightMax[n-1]=sumMax;
        rightMin[n-1]=sumMin;
        for(int i=n-2;i>=0;i--)
        {
            sumMax=Math.max(sumMax+nums[i],nums[i]);
            rightMax[i]=Math.max(rightMax[i+1],sumMax);

            sumMin=Math.min(sumMin+nums[i],nums[i]);
            rightMin[i]=Math.min(rightMin[i+1],sumMin);
        }

        for(int i=0;i<n-1;i++)
        {
            if(res<Math.abs(leftMax[i]-rightMin[i+1]))
            res=Math.abs(leftMax[i]-rightMin[i+1]);

            if(res<Math.abs(leftMin[i]-rightMax[i+1]))
            res=Math.abs(leftMin[i]-rightMax[i+1]);
        }

        return res;
    }
}
时间: 2024-10-17 08:34:03

[lintcode medium]Maximum Subarray Difference的相关文章

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

Lintcode: Maximum Subarray Difference

Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Note The subarray should contain at least one number Example For [1, 2, -3, 1], return 6 Challenge O(n) t

lintcode 中等题:maximum subarray difference 最大子数组差

题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1],返回 6 注意 子数组最少包含一个数 挑战 时间复杂度为O(n),空间复杂度为O(n) 解题 刚做了数组中两个子数组和的最大值,这一题是求差,感觉上题的求解思想应该是可以用的 A B 分别是两个子数组的和,则: 所以 当A>B 的时候A越大越好 B越小越好 当A<B 的时候B越大越好 A越小越好

Maximum Subarray Difference

该题的思路是使用辅助数组来保存从前向后和从后向前到当前位置的最大最小值,这样就可以通过每一个位置处的最大最小值来计算该位置的结果,最后在所有结果中选出最大的值即可. int maxDiffSubArrays(vector<int> nums) { // write your code here int size = nums.size(); //以数组中数的个数为维度,定义四个辅助数组 int *maxArray = new int[size]; int *preMinArray = new

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.