LintCode "Continuous Subarray Sum II"

Flip over your mind: in rotated subarray case, we can simply cut the continuous smallest subarray.

class Solution {
public:
    /**
     * @param A an integer array
     * @return  A list of integers includes the index of
     *          the first number and the index of the last number
     */

    vector<int> continuousSubarraySumII(vector<int>& A) {
        vector<int> ret;

        size_t len = A.size();

        long long sum = A[0], csum = A[0];
        int s = 0, e = 0, ss = 0, ee = 0; // for max continuous

        long long sum0= A[0], csum0= A[0];
        int s0= 0, e0= 0, ss0= 0, ee0= 0; // for min continuous

        bool bAllNeg = true;
        for (int i = 1; i < len; i++)
        {
            if(A[i] >= 0) bAllNeg = false;
            //  max
            long long nsum = csum + A[i];
            if (A[i] > nsum)
            {
                ss = ee = i;
                csum = A[i];
            }
            else
            {
                ee = i;
                csum = nsum;
            }

             if(csum > sum)
            {
                sum = csum;
                s = ss;
                e = ee;
            }

            //  min
            long long nsum0 = csum0 + A[i];
            if (A[i] < nsum0)
            {
                ss0 = ee0 = i;
                csum0= A[i];
            }
            else
            {
                ee0 = i;
                csum0= nsum0;
            }
            if(csum0 < sum0)
            {
                sum0 = csum0;
                s0 = ss0;
                e0 = ee0;
            }
        }

        long long asum = accumulate(A.begin(), A.end(), 0);
        long long osum = asum - sum0;
        if (bAllNeg)
        {
            int inx = max_element(A.begin(), A.end()) - A.begin();
            ret.push_back(inx);
            ret.push_back(inx);
        }
        else if (sum >= osum)
        {
            ret.push_back(s);
            ret.push_back(e);
        }
        else
        {
            ret.push_back(e0 + 1);
            ret.push_back(s0 - 1);
        }
        return ret;
    }
};
时间: 2024-12-11 06:56:40

LintCode "Continuous Subarray Sum II"的相关文章

Continuous Subarray Sum II(LintCode)

Continuous Subarray Sum II Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the index of the first number a

Continuous Subarray Sum II

Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last nu

[LintCode] Continuous Subarray Sum 连续子数组之和

Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone) Have you met this quest

LintCode &quot;Continuous Subarray Sum&quot;

A variation to a classical DP: LCS. class Solution { public: /** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the last number */ vector<int> continuousSubarraySum(vector<int>

[LintCode] Subarray Sum &amp; Subarray Sum II

Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number. Example Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3]. 用hashmap来存从nums[0

LeetCode:Continuous Subarray Sum

523. Continuous Subarray Sum Add to List Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where

Continuous Subarray Sum

Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone) 和Maximum Subarray的差别在于仅

leetcode 523. Continuous Subarray Sum

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer. Example 1: Input:

523. Continuous Subarray Sum

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer. Example 1: Input: