Memory Limit Exceeded


  • 1
    A

    I am getting Memory Limit Exceeded but not getting why??

    class Solution {
        public:
            static bool compare(const pair <int,int> &a, const pair <int,int> &b)
            {
                return a.first < b.first;
            }
            vector<int> twoSum(vector<int> &numbers, int target) {
                int index1=0,index2=numbers.size()-1;
                vector <pair<int,int> > v;
                for(int i=0;i<numbers.size();i++)
                {
                    v.push_back(make_pair(numbers[i],i+1));
                }
                sort(v.begin(),v.end(),compare);
                vector <int> res;
                while(index2>index1)
                {
                    if(v[index1].first+v[index2].first > target) index2--;
                    else if(v[index1].first+v[index2].first < target) index1++;
                    else
                    {
                        res.push_back(v[index1].second);
                        res.push_back(v[index2].second);
                    }
                }
                return res;
            }
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.