TLE C++ solution. Hope it helps, at all. First time trying to give back to the community.
Hi, I hope this helps anyone trying to start this problem. I\'m a weak programmer and didn\'t quite figure out a good solution yet. But I just want to post my n
Hi, I hope this helps anyone trying to start this problem. I\'m a weak programmer and didn\'t quite figure out a good solution yet. But I just want to post my naive TLE solution here. First time trying to give back to this community. XD\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        if(arr.size() < 2)\n            return -1;\n        unordered_map<int, int> m;\n        vector<pair<int, int> > subs;\n        m[0] = -1;\n        int sum = 0;\n        for(int i=0; i<arr.size(); i++){\n            sum+=arr[i];\n            if(m.count(sum-target)){\n                subs.push_back(make_pair(m[sum-target], i));\n            }\n            m[sum] = i;\n        }\n        auto comp = [](pair<int, int>& a, pair<int, int>& b){return (a.second-a.first) < (b.second-b.first); };\n        sort(subs.begin(), subs.end(), comp);\n        \n        // for(auto& a:subs){\n        //     cout << a.first << " " << a.second << endl;\n        // }\n        for(int i=0; i<subs.size(); i++){\n            for(int j=i+1; j<subs.size(); j++){\n                if(noOver(subs[i], subs[j]))\n                    return (subs[i].second-subs[i].first + subs[j].second - subs[j].first);\n            }\n        }\n        return -1;\n    }\n    \n    bool noOver(pair<int, int>& a, pair<int, int>& b){\n        if( (a.first <= b.first && a.second > b.first)\n            || (a.first >= b.first && a.first < b.second) )\n            return false;\n        return true;\n            \n    }\n\n};\n```\n\nupdate:\nIf anyone is interested, I actually figured out how to solve this. I don\'t quite need to sort the vector, but rather I could keep the shortest subarray while I go over the array.\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        if(arr.size() < 2)\n            return -1;\n        unordered_map<int, int> m;\n        vector<pair<int, int> > subs;\n        m[0] = -1;\n        int sum = 0;\n        int shortest_len = INT_MAX;\n        pair<int, int> shortest = {-1, -1};\n        for(int i=0; i<arr.size(); i++){\n            sum+=arr[i];\n            if(m.count(sum-target)){\n                subs.push_back(make_pair(m[sum-target], i));\n                if(i-m[sum-target] < shortest_len){\n                    shortest_len = i-m[sum-target];\n                    shortest = make_pair(m[sum-target], i);\n                }\n            }\n            m[sum] = i;\n        }\n        if(shortest_len == INT_MAX)\n            return -1;\n        int second_shortest_len = INT_MAX;\n        pair<int, int> second_shortest = {-1, -1};\n        for(int i=0; i<subs.size(); i++){\n            if(noOver(subs[i], shortest)){\n                if((subs[i].second - subs[i].first ) < second_shortest_len){\n                    second_shortest_len = subs[i].second - subs[i].first;\n                    second_shortest = subs[i];\n                }\n            }\n        }\n         if(second_shortest_len == INT_MAX)\n            return -1;\n        return (shortest.second-shortest.first + second_shortest.second - second_shortest.first);\n    }\n    \n    bool noOver(pair<int, int>& a, pair<int, int>& b){\n        if( (a.first <= b.first && a.second > b.first)\n            || (a.first >= b.first && a.first < b.second) )\n            return false;\n        return true;\n    }\n};\n```\nOnly beats 20%. But still being proud of it.\n\n2nd update:\nI don\'t think there\'s really a way to solve this with my intuition. (Not confident at all).\nMy second solution couldn\'t pass testcases for [1,2,3,3,2,1] because it assumes [3,3] is a part of the final answer. My first solution could solve the problem, which makes it O(n^2) though. Sad face.\n\n(and [1,2,3,3,2,1] is not part of the test cases given.)
['C']