LeetCode "Maximum Size Subarray Sum Equals k"

Nothing special. Point is, how clean can you do it?

class Solution 
{
public:
    int maxSubArrayLen(vector<int>& nums, int k) 
    {
        int n = nums.size();
        
        //    Partial Sum
        vector<int> asum(n);
        partial_sum(nums.begin(), nums.end(), asum.begin());

        int ret = 0;
        unordered_map<int, int> hm; // asum - min inx
        hm[0] = -1;
        for (int i = 0; i < n; i++)
        {
            int tgt = asum[i] - k;
            if (hm.count(tgt))    ret = max(ret, i - hm[tgt]);
            if (!hm.count(asum[i]))    hm[asum[i]] = i;
        }
        return ret;
    }
};

你可能感兴趣的:(LeetCode "Maximum Size Subarray Sum Equals k")