[C++] sliding window  + comments [WARNING: test cases missing]
Idea: \nFind two sub-arrays to meet the following two conditions:\n1) sum(sub_array1) == target && sum(sub_array2) == target\n2) min_of_all_possible_comibantion
**Idea**: \nFind two sub-arrays to meet the following two conditions:\n1) sum(sub_array1) == target && sum(sub_array2) == target\n2) min_of_all_possible_comibantion(len1 + len2), where len1= length(sub_array1), len2= length(sub_array2), len1 and len2 are the smallest two subarrays meet the condition 1.\nNote:\nmin_of_all_possible(len1 + len2)  =/= min_of_all_possible(len1) + min_of_all_possible(len2)\n\n\n\n\n\n\nVersion 1: Sliding Window Time/Space: O(N) / O(N)\n```\nclass Solution { // Sliding window; Original author: huahua[1]\npublic: // Time/Space: O(N); O(N)\n    int minSumOfLengths(vector<int>& arr, int target) {\n        const int n = arr.size(), kInf = INT_MAX/2;\n        int ans = kInf;\n        vector<int> memo(n, kInf); //memo[i] := min length of a valid subarray ends or before i.\n        for(int i = 0, sum = 0, s=0, min_len = kInf; i < n; i++){\n            sum += arr[i];\n            while(sum > target) sum -= arr[s++]; //shrink from left, \'s\'\n            if(sum == target){                \n                int cur_len = i - s + 1;\n                if( s > 0 && memo[s-1] != kInf)\n                    ans = min(ans, cur_len + memo[s-1]);\n                min_len = min(min_len, cur_len);\n            }\n            memo[i]=min_len;\n        }        \n        return ans < kInf ? ans : -1;\n    }\n};\n```\nReference:\n[1] https://zxi.mytechroad.com/blog/sliding-window/leetcode-1477-find-two-non-overlapping-sub-arrays-each-with-target-sum/\n\nOkay, I keep thinking for hours that do we really need keep memo, or the information for the minimal  length of the desired subarray (length and ending index) good enough? It turns out by far I still need memo to track all possible comibination. \n\nThen I provided the 2nd version, which is surpisingly but not that surpsingly wrong. @Chernik provided an good example where this solution might fail:\n\nWhy? This is not a greedy question.\n```\nmin_of_all_possible(len1 + len2)  != min_of_all_possible(len1) + min_of_all_possible(len2)\n```\n len1 is from the past, len2 is from the current sub-array when you scan from left to right.\n\nVersion 2: Sliding Window Time/Space: O(N) / O(1)  (This version is wrong.)\n```\nclass Solution { // Sliding window: Wrong Solution. \npublic: // Time/Space: O(N); O(1)\n    int minSumOfLengths(vector<int>& arr, int target) {\n        const int n = arr.size(), kInf = INT_MAX/2;\n        int ans = kInf;        \n        for(int i = 0, sum = 0, s=0, min_len = kInf, last_end = 0; i < n; i++){\n            sum += arr[i];\n            while(sum > target) sum -= arr[s++]; //shrink from left, \'s\'\n            if(sum == target){                \n                int cur_len = i - s + 1;                \n                if( min_len < kInf && last_end < s)//already seen one slidwing window with min_len in the past, which dose not overlap with current one\n                    ans = min(ans, cur_len + min_len);                \n                if(cur_len < min_len){\n                    last_end = i; // ending index of the minimum length window by far\n                    min_len = cur_len;\n                }            \n            }            \n        }        \n        return ans < kInf ? ans : -1;\n    }\n};\n```\n[1,1,1,2,1,1]\n3\n```\n\nP.S.:\nIf you like the idea here, please help upvote since it takes hours to optmize.\nIf you do not like the idea here, please let me know so that I can optimize myself to provide better solution in future.
['Greedy', 'C', 'Sliding Window']