Maximum Subarray Difference

该题的思路是使用辅助数组来保存从前向后和从后向前到当前位置的最大最小值,这样就可以通过每一个位置处的最大最小值来计算该位置的结果,最后在所有结果中选出最大的值即可。

    int maxDiffSubArrays(vector<int> nums) {
        // write your code here
        int size = nums.size();

//以数组中数的个数为维度,定义四个辅助数组
        int *maxArray = new int[size];
        int *preMinArray = new int[size];
        int *minArray = new int[size];
        int *postMaxArray = new int[size];

//填充辅助数组的值
        maxArray[0] = nums[0];
        preMinArray[0] = nums[0];
        for(int i = 1; i < size; i++){
            maxArray[i] = max(nums[i], nums[i] + maxArray[i - 1]);
            preMinArray[i] = min(nums[i], nums[i] + preMinArray[i - 1]);
        }

        minArray[size - 1] = nums[size - 1];
        minArray[size - 2] = nums[size - 1];
        postMaxArray[size - 1] = nums[size - 1];
        postMaxArray[size - 2] = nums[size - 1];

        for(int i = size - 3; i >= 0; i--){
            minArray[i] = min(nums[i + 1], nums[i + 1] + minArray[i + 1]);
            postMaxArray[i] = max(nums[i + 1], nums[i + 1] + postMaxArray[i + 1]);
        }

//利用四个辅助数组中的值计算最终的结果
        int result = abs(maxArray[0] - minArray[0]);
        for(int i = 0; i < size - 1; i++){
            if(abs(maxArray[i] - minArray[i]) > result)
                result = abs(maxArray[i] - minArray[i]);
            if(abs(preMinArray[i] - postMaxArray[i]) > result)
                result = abs(preMinArray[i] - postMaxArray[i]);
        }

//删除辅助数组
        delete []maxArray;
        delete []minArray;
        delete []preMinArray;
        delete []postMaxArray;

        return result;
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-16 14:14:34

Maximum Subarray Difference的相关文章

[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 leas

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越小越好

[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.

41. leetcode 53. Maximum Subarray

53. 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. 思路:这个题还挺经典

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. 原题链接: https://oj.leetcode.com/p

Codechef Maximum Weight Difference题解

Maximum Weight Difference Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants hi