Problem with Leetcode online judge

I submitted the following code two times one after the other for the problem Two Sum.

vector<int> twoSum(vector<int>& v, int target) {
		vector<pair<int,int>> v1(v.size());
        for(int i = 0; i < v.size(); i++)
            v1[i] = {v[i], i};
        sort(v1.begin(), v1.end(), [](pair<int,int>&a, pair<int,int>&b){return a.first < b.first;});
        auto first = v1.begin();
        auto last = v1.end() - 1;
        while(first != last){
            auto sum = first->first + last->first;
            if(sum == target)
            return {first->second, last->second};
            if(sum > target)
                last--;
            else first++;
        }
        return {-1,-1};
}

The runtimes were drastically different in both cases: first 4ms, second 12ms. Posting again showed 4ms again.
Similar discrepency happened with Squares of a sorted array with following submission:

vector<int> sortedSquares(vector<int>& nums) {
        vector<int> res(nums.size());
        auto positiveBeginIter = lower_bound(nums.begin(), nums.end(), 0);
        auto negativeReverseBeginIter = make_reverse_iterator(positiveBeginIter);
        for_each(nums.begin(), nums.end(), [](int&e){e *= e;});
        merge(positiveBeginIter, nums.end(), negativeReverseBeginIter, nums.rend(), res.begin());
        return res;
    }

It showed 16ms initially, then again 40ms next time, without any change in code.
Can this be fixed please? Or can I get a reason why this happens and if this is expected?

Comments (0)